Test Failed
Push — feature-laravel-5.4 ( 2edebe...f95f7c )
by Kirill
03:06
created

DataProvidersImport::getLatestBotArticles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of laravel.su package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace App\Console\Commands;
11
12
use App\Models\Article;
13
use App\Models\Bot;
14
use App\Services\DataProviders\DataProviderInterface;
15
use App\Services\DataProviders\ExternalArticle;
16
use App\Services\DataProviders\Manager;
17
use Carbon\Carbon;
18
use Illuminate\Console\Command;
19
use Illuminate\Database\Eloquent\Relations\MorphMany;
20
21
/**
22
 * Class DataProvidersImport.
23
 */
24
class DataProvidersImport extends Command
25
{
26
    /**
27
     * The name and signature of the console command.
28
     * @var string
29
     */
30
    protected $signature = 'data-providers:import';
31
32
    /**
33
     * The console command description.
34
     * @var string
35
     */
36
    protected $description = 'Run import articles from external services';
37
38
    /**
39
     * @var array
40
     */
41
    private $published = [];
42
43
    /**
44
     * @param  Manager $manager
45
     * @throws \InvalidArgumentException
46
     */
47
    public function handle(Manager $manager)
48
    {
49
        //$this->importPublished($manager);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
51
        $this->importNotPublished($manager);
52
    }
53
54
    /**
55
     * @param Manager $manager
56
     * @return void
57
     */
58
    private function importNotPublished(Manager $manager): void
59
    {
60
        foreach ($manager as $alias => $provider) {
61
            if (in_array($alias, $this->published, true)) {
62
                continue;
63
            }
64
65
            $this->import(Carbon::createFromTimestamp(0), $provider);
66
        }
67
    }
68
69
    /**
70
     * @param \DateTime             $time
71
     * @param DataProviderInterface $provider
72
     */
73
    private function import(\DateTime $time, DataProviderInterface $provider)
74
    {
75
        /** @var ExternalArticle[] $latest */
76
        $latest = $provider->getLatest($time);
77
78
        foreach ($latest as $external) {
79
            $article = new Article();
80
81
            $article->user_id = 1;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<App\Models\Article>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
            $article->user_type = Bot::class;
0 ignored issues
show
Documentation introduced by
The property user_type does not exist on object<App\Models\Article>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
83
84
            $external->fill($article);
85
86
            $article->save();
87
        }
88
    }
89
90
    /**
91
     * @param Manager $manager
92
     * @throws \InvalidArgumentException
93
     * @return void
94
     */
95
    private function importPublished(Manager $manager): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
96
    {
97
        /** @var Article $article */
98
        foreach ($this->getLatestBotArticles() as $article) {
99
            /** @var Bot $bot */
100
            $bot = $article->user;
101
102
            $this->import($article->published_at, $manager->get($bot->provider));
0 ignored issues
show
Documentation introduced by
The property provider does not exist on object<App\Models\Bot>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
104
            $this->published[] = $bot->provider;
0 ignored issues
show
Documentation introduced by
The property provider does not exist on object<App\Models\Bot>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
105
        }
106
    }
107
108
    /**
109
     * @return \Generator|Article[]
110
     */
111
    private function getLatestBotArticles(): \Generator
112
    {
113
        $bots = Bot::query()
114
            ->with(['articles' => function (MorphMany $relation) {
115
                return $relation->latest('published_at')->take(1);
116
            }])
117
            ->get();
118
119
        foreach ($bots as $bot) {
120
            $article = $bot->articles->first();
121
122
            if ($article) {
123
                yield $article;
124
            }
125
        }
126
    }
127
}
128