<commit_before><?php namespace MicheleAngioni\MessageBoard\Services;

use Helpers;
use Illuminate\Support\Collection;
use MicheleAngioni\MessageBoard\Contracts\CommentRepositoryInterface as CommentRepo;
use MicheleAngioni\MessageBoard\Contracts\LikeRepositoryInterface as LikeRepo;
use MicheleAngioni\MessageBoard\Contracts\MbUserInterface;
use MicheleAngioni\MessageBoard\Contracts\PostRepositoryInterface as PostRepo;
use MicheleAngioni\MessageBoard\Contracts\PurifierInterface;
use MicheleAngioni\MessageBoard\Contracts\ViewRepositoryInterface as ViewRepo;
use MicheleAngioni\MessageBoard\Events\CommentCreate;
use MicheleAngioni\MessageBoard\Events\CommentDelete;
use MicheleAngioni\MessageBoard\Events\LikeCreate;
use MicheleAngioni\MessageBoard\Events\LikeDestroy;
use MicheleAngioni\MessageBoard\Events\PostCreate;
use MicheleAngioni\MessageBoard\Events\PostDelete;
use MicheleAngioni\MessageBoard\Events\UserBanned;
use MicheleAngioni\MessageBoard\Models\Comment;
use MicheleAngioni\MessageBoard\Models\Like;
use MicheleAngioni\MessageBoard\Models\Post;
use MicheleAngioni\MessageBoard\Presenters\CommentPresenter;
use MicheleAngioni\MessageBoard\Presenters\PostPresenter;
use MicheleAngioni\Support\Presenters\Presenter;
use Event;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use InvalidArgumentException;
use MicheleAngioni\Support\Exceptions\PermissionsException;
use RuntimeException;

class MessageBoardService {

    /**
     * @var CommentRepo
     */
    protected $commentRepo;

    /**
     * Entities a like can be associated to and relative repositories.
     *
     * @var array
     */
    protected $likableEntities = [
        'comment' => 'commentRepo',
        'post' => 'postRepo'
    ];

    /**
     * @var LikeRepo
     */
    protected $likeRepo;

    /**
     * Standard number of mb posts per page.
     *
     * @var int
     */
    protected $postsPerPage;

    /**
     * @var PostRepo
     */
    protected $postRepo;

    protected $presenter;

    protected $purifier;

    /**
     * @var ViewRepo
     */
    protected $viewRepo;


    function __construct(CommentRepo $commentRepo, LikeRepo $likeRepo, PostRepo $postRepo, Presenter $presenter,
                         PurifierInterface $purifier, ViewRepo $viewRepo)
    {
        $this->commentRepo = $commentRepo;

        $this->likeRepo = $likeRepo;

        $this->postsPerPage = config('ma_messageboard.posts_per_page');

        $this->postRepo = $postRepo;

        $this->presenter = $presenter;

        $this->purifier = $purifier;

        $this->viewRepo = $viewRepo;
    }

    /**
     * Create the coded text of the mb post.
     * Keys in the messageboard.php lang file will be used for localization.
     *
     * @param  string  $code
     * @param  MbUserInterface  $user
     * @param  array  $variables
     * @throws InvalidArgumentException
     *
     * @return string
     */
    public function getCodedPostText($code, MbUserInterface $user = null, array $variables = [])
    {
        if(!$code) {
            throw new InvalidArgumentException('InvalidArgumentException in '.__METHOD__.' at line '.__LINE__.': No input code.');
        }

        // Set user attribute, is set

        if($user) {
            $variables['user'] =  $this->getUserLink($user);
        }

        if(config('ma_messageboard.localization_file_name')) {
            return $this->mbText = trans(config('ma_messageboard.localization_file_name.') . $code, $variables);
        }

        return $this->mbText = trans('ma_messageboard::messageboard.'.$code, $variables);
    }

    /**
     * Create a new Post from a coded text.
     *
     * @param  MbUserInterface  $user
     * @param  int|null  $categoryId
     * @param  string  $code
     * @param  array  $attributes
     *
     * @return Post
     */
    public function createCodedPost(MbUserInterface $user, $categoryId = null, $code, array $attributes = [])
    {
        $data = [
            'category_id' => $categoryId,
            'user_id' => $user->getPrimaryId(),
            'text' => $this->getCodedPostText($code, $user, $attributes)
        ];

        $post = $this->postRepo->create($data);

        // Fire event
        Event::fire(new PostCreate($post));

        return $post;<commit_msg>Fixed Coded Posts with custom lang file<commit_after><?php namespace MicheleAngioni\MessageBoard\Services;

use Helpers;
use Illuminate\Support\Collection;
use MicheleAngioni\MessageBoard\Contracts\CommentRepositoryInterface as CommentRepo;
use MicheleAngioni\MessageBoard\Contracts\LikeRepositoryInterface as LikeRepo;
use MicheleAngioni\MessageBoard\Contracts\MbUserInterface;
use MicheleAngioni\MessageBoard\Contracts\PostRepositoryInterface as PostRepo;
use MicheleAngioni\MessageBoard\Contracts\PurifierInterface;
use MicheleAngioni\MessageBoard\Contracts\ViewRepositoryInterface as ViewRepo;
use MicheleAngioni\MessageBoard\Events\CommentCreate;
use MicheleAngioni\MessageBoard\Events\CommentDelete;
use MicheleAngioni\MessageBoard\Events\LikeCreate;
use MicheleAngioni\MessageBoard\Events\LikeDestroy;
use MicheleAngioni\MessageBoard\Events\PostCreate;
use MicheleAngioni\MessageBoard\Events\PostDelete;
use MicheleAngioni\MessageBoard\Events\UserBanned;
use MicheleAngioni\MessageBoard\Models\Comment;
use MicheleAngioni\MessageBoard\Models\Like;
use MicheleAngioni\MessageBoard\Models\Post;
use MicheleAngioni\MessageBoard\Presenters\CommentPresenter;
use MicheleAngioni\MessageBoard\Presenters\PostPresenter;
use MicheleAngioni\Support\Presenters\Presenter;
use Event;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use InvalidArgumentException;
use MicheleAngioni\Support\Exceptions\PermissionsException;
use RuntimeException;

class MessageBoardService {

    /**
     * @var CommentRepo
     */
    protected $commentRepo;

    /**
     * Entities a like can be associated to and relative repositories.
     *
     * @var array
     */
    protected $likableEntities = [
        'comment' => 'commentRepo',
        'post' => 'postRepo'
    ];

    /**
     * @var LikeRepo
     */
    protected $likeRepo;

    /**
     * Standard number of mb posts per page.
     *
     * @var int
     */
    protected $postsPerPage;

    /**
     * @var PostRepo
     */
    protected $postRepo;

    protected $presenter;

    protected $purifier;

    /**
     * @var ViewRepo
     */
    protected $viewRepo;


    function __construct(CommentRepo $commentRepo, LikeRepo $likeRepo, PostRepo $postRepo, Presenter $presenter,
                         PurifierInterface $purifier, ViewRepo $viewRepo)
    {
        $this->commentRepo = $commentRepo;

        $this->likeRepo = $likeRepo;

        $this->postsPerPage = config('ma_messageboard.posts_per_page');

        $this->postRepo = $postRepo;

        $this->presenter = $presenter;

        $this->purifier = $purifier;

        $this->viewRepo = $viewRepo;
    }

    /**
     * Create the coded text of the mb post.
     * Keys in the messageboard.php lang file will be used for localization.
     *
     * @param  string  $code
     * @param  MbUserInterface  $user
     * @param  array  $variables
     * @throws InvalidArgumentException
     *
     * @return string
     */
    public function getCodedPostText($code, MbUserInterface $user = null, array $variables = [])
    {
        if(!$code) {
            throw new InvalidArgumentException('InvalidArgumentException in '.__METHOD__.' at line '.__LINE__.': No input code.');
        }

        // Set user attribute, is set

        if($user) {
            $variables['user'] =  $this->getUserLink($user);
        }

        if(config('ma_messageboard.localization_file_name')) {
            return $this->mbText = trans(config('ma_messageboard.localization_file_name') . '.' . $code, $variables);
        }

        return $this->mbText = trans('ma_messageboard::messageboard.'.$code, $variables);
    }

    /**
     * Create a new Post from a coded text.
     *
     * @param  MbUserInterface  $user
     * @param  int|null  $categoryId
     * @param  string  $code
     * @param  array  $attributes
     *
     * @return Post
     */
    public function createCodedPost(MbUserInterface $user, $categoryId = null, $code, array $attributes = [])
    {
        $data = [
            'category_id' => $categoryId,
            'user_id' => $user->getPrimaryId(),
            'text' => $this->getCodedPostText($code, $user, $attributes)
        ];

        $post = $this->postRepo->create($data);

        // Fire event
        Event::fire(new PostCreate($post));

        return $post;
{'fraction_non_alphanumeric': 0.1072301781348236, 'fraction_numerical': 0.0, 'mean_word_length': 3.0290806754221387, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3227429', 'n_tokens_mistral': 2513, 'n_tokens_neox': 2506, 'n_words': 523}