<commit_before><?php

namespace Coyote\Console\Commands;

use Coyote\Http\Factories\MailFactory;
use Coyote\Repositories\Contracts\UserRepositoryInterface as UserRepository;
use Coyote\Services\Elasticsearch\ResultSet;
use Coyote\User;
use Elasticsearch\Client;
use Illuminate\Console\Command;
use Illuminate\Mail\Message;

class PurgeJobsCommand extends Command
{
    use MailFactory;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'job:purge';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Purge expired job offers from Elasticsearch index.';

    /**
     * @var Client
     */
    protected $elasticsearch;

    /**
     * @var UserRepository
     */
    protected $user;

    /**
     * @var array
     */
    private $params = [];

    /**
     * @param UserRepository $user
     */
    public function __construct(UserRepository $user)
    {
        parent::__construct();

        $this->elasticsearch = app('elasticsearch');
        $this->params = [
            'index' => config('elasticsearch.default_index'),
            'type'  => 'jobs',
            'body'  => [
                'query' => [
                    'bool' => [
                        'must' => [
                            ['range' => ['deadline_at' => ['lt' => 'now']]]
                        ]
                    ]
                ],
                'size' => 100
            ]
        ];

        $this->user = $user;
    }

    /**
     * @return void
     */
    public function handle()
    {
        $result = new ResultSet($this->elasticsearch->search($this->params));

        foreach ($result as $hit) {
            $this->elasticsearch->delete(
                ['id' => $hit['id'], 'index' => config('elasticsearch.default_index'), 'type' => 'jobs']
            );

            $user = $this->user->find($hit['user_id'], ['name', 'email']);

            if ($user !== null && $user->email) {
                $this->sendEmail($user, $hit);
                $this->info(sprintf('Sending e-mail about ending offer: %s.', $hit['title']));
            }
        }
    }

    /**
     * @param User $user
     * @param array $hit
     */
    private function sendEmail(User $user, array $hit)
    {
        $this->getMailFactory()->queue('emails.job.expired', $hit, function (Message $message) use ($hit, $user) {
            $message->subject('Twoje ogłoszenie "' . $hit['title'] . '" wygasło.');
            $message->to($user->email);
        });
    }
}<commit_msg>migracja na laravel 5.4<commit_after><?php

namespace Coyote\Console\Commands;

use Coyote\Http\Factories\MailFactory;
use Coyote\Repositories\Contracts\UserRepositoryInterface as UserRepository;
use Coyote\Services\Elasticsearch\ResultSet;
use Coyote\User;
use Elasticsearch\Client;
use Illuminate\Console\Command;
use Illuminate\Mail\Message;

class PurgeJobsCommand extends Command
{
    use MailFactory;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'job:purge';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Purge expired job offers from Elasticsearch index.';

    /**
     * @var Client
     */
    protected $elasticsearch;

    /**
     * @var UserRepository
     */
    protected $user;

    /**
     * @var array
     */
    private $params = [];

    /**
     * @param UserRepository $user
     */
    public function __construct(UserRepository $user)
    {
        parent::__construct();

        $this->elasticsearch = app('elasticsearch');
        $this->params = [
            'index' => config('elasticsearch.default_index'),
            'type'  => 'jobs',
            'body'  => [
                'query' => [
                    'bool' => [
                        'must' => [
                            ['range' => ['deadline_at' => ['lt' => 'now']]]
                        ]
                    ]
                ],
                'size' => 100
            ]
        ];

        $this->user = $user;
    }

    /**
     * @return void
     */
    public function handle()
    {
        $result = new ResultSet($this->elasticsearch->search($this->params));

        foreach ($result as $hit) {
            $this->elasticsearch->delete(
                ['id' => $hit['id'], 'index' => config('elasticsearch.default_index'), 'type' => 'jobs']
            );

            $user = $this->user->find($hit['user_id'], ['name', 'email']);

            if ($user !== null && $user->email) {
                $this->sendEmail($user, $hit);
                $this->info(sprintf('Sending e-mail about ending offer: %s.', $hit['title']));
            }
        }
    }

    /**
     * @param User $user
     * @param array $hit
     */
    private function sendEmail(User $user, array $hit)
    {
        // od laravel 5.4 nie moze byc queue(). musimy uzyc send()
        $this->getMailFactory()->send('emails.job.expired', $hit, function (Message $message) use ($hit, $user) {
            $message->subject('Twoje ogłoszenie "' . $hit['title'] . '" wygasło.');
            $message->to($user->email);
        });
    }
}
{'fraction_non_alphanumeric': 0.15642775881683732, 'fraction_numerical': 0.0018960940462646946, 'mean_word_length': 1.7763157894736843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, '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': '6258690', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1502, 'n_words': 319}