<commit_before><?php

namespace Coyote\Services\Microblogs;

use Coyote\Microblog;
use Coyote\Models\Scopes\UserRelationsScope;
use Coyote\Repositories\Contracts\MicroblogRepositoryInterface;
use Coyote\Repositories\Criteria\Microblog\LoadUserScope;
use Coyote\Repositories\Criteria\Microblog\OnlyMine;
use Coyote\Repositories\Criteria\Microblog\OrderById;
use Coyote\Repositories\Criteria\Microblog\OrderByScore;
use Coyote\Repositories\Criteria\Microblog\WithTag;
use Coyote\Repositories\Criteria\WithoutScope;
use Illuminate\Contracts\Auth\Guard;
use Coyote\User;

class Builder
{
    /**
     * @var MicroblogRepositoryInterface
     */
    private MicroblogRepositoryInterface $microblog;

    /**
     * @var User|null
     */
    private ?User $user;

    private bool $isSponsor = false;

    public function __construct(MicroblogRepositoryInterface $microblog, Guard $auth)
    {
        $this->microblog = $microblog;
        $this->user = $auth->user();

        if ($this->user) {
            $this->isSponsor = $this->user->is_sponsor;
        }
    }

    /**
     * @return $this
     */
    public function orderByScore()
    {
        $this->microblog->pushCriteria(new OrderByScore(!$this->isSponsor));

        return $this;
    }

    /**
     * @return $this
     */
    public function orderById()
    {
        $this->microblog->pushCriteria(new OrderById(!$this->isSponsor));

        return $this;
    }

    /**
     * @param User $user
     * @return $this
     */
    public function onlyUsers(User $user)
    {
        $this->microblog->pushCriteria(new OnlyMine($user->id));
        $this->microblog->pushCriteria(new WithoutScope(UserRelationsScope::class));

        return $this;
    }

    /**
     * @param string $tag
     * @return $this
     */
    public function withTag(string $tag)
    {
        $this->microblog->pushCriteria(new WithTag($tag));

        return $this;
    }

    /**
     * @return \Illuminate\Pagination\LengthAwarePaginator
     */
    public function paginate()
    {
        $this->loadUserScope();

        $paginator = $this->microblog->paginate(10);<commit_msg>optymalizacja: zapytanie COUNT(*) nie musi zawierac left join<commit_after><?php

namespace Coyote\Services\Microblogs;

use Coyote\Microblog;
use Coyote\Models\Scopes\UserRelationsScope;
use Coyote\Repositories\Contracts\MicroblogRepositoryInterface;
use Coyote\Repositories\Criteria\Microblog\LoadUserScope;
use Coyote\Repositories\Criteria\Microblog\OnlyMine;
use Coyote\Repositories\Criteria\Microblog\OrderById;
use Coyote\Repositories\Criteria\Microblog\OrderByScore;
use Coyote\Repositories\Criteria\Microblog\WithTag;
use Coyote\Repositories\Criteria\WithoutScope;
use Illuminate\Contracts\Auth\Guard;
use Coyote\User;
use Illuminate\Pagination\LengthAwarePaginator;

class Builder
{
    /**
     * @var MicroblogRepositoryInterface
     */
    private MicroblogRepositoryInterface $microblog;

    /**
     * @var User|null
     */
    private ?User $user;

    private bool $isSponsor = false;

    public function __construct(MicroblogRepositoryInterface $microblog, Guard $auth)
    {
        $this->microblog = $microblog;
        $this->user = $auth->user();

        if ($this->user) {
            $this->isSponsor = $this->user->is_sponsor;
        }
    }

    /**
     * @return $this
     */
    public function orderByScore()
    {
        $this->microblog->pushCriteria(new OrderByScore(!$this->isSponsor));

        return $this;
    }

    /**
     * @return $this
     */
    public function orderById()
    {
        $this->microblog->pushCriteria(new OrderById(!$this->isSponsor));

        return $this;
    }

    /**
     * @param User $user
     * @return $this
     */
    public function onlyUsers(User $user)
    {
        $this->microblog->pushCriteria(new OnlyMine($user->id));
        $this->microblog->pushCriteria(new WithoutScope(UserRelationsScope::class));

        return $this;
    }

    /**
     * @param string $tag
     * @return $this
     */
    public function withTag(string $tag)
    {
        $this->microblog->pushCriteria(new WithTag($tag));

        return $this;
    }

    /**
     * @return \Illuminate\Pagination\LengthAwarePaginator
     */
    public function paginate()
    {
        $this->microblog->applyCriteria();

        $count = $this->microblog->count();
        $page = LengthAwarePaginator::resolveCurrentPage();

        $this->loadUserScope();
        $perPage = 10;

        $result = $this->microblog->limit($perPage)->offset(max(0, $page - 1) * $perPage)->get();

        $paginator = new LengthAwarePaginator(
            $result,
            $count,
            $perPage,
            $page,
            ['path' => LengthAwarePaginator::resolveCurrentPath()]
        );

{'fraction_non_alphanumeric': 0.13017999162829635, 'fraction_numerical': 0.0012557555462536626, 'mean_word_length': 2.72196261682243, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 56, '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': '4502807', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1461, 'n_words': 245}