Test Failed
Push — feature-laravel-5.4 ( 475d96...446986 )
by Kirill
07:30
created

DataProvidersImport   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 21 2
A importNotPublished() 0 10 3
A import() 0 16 2
1
<?php
2
3
/**
4
 * This file is part of laravel.su package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace App\Console\Commands;
12
13
use App\Models\Article;
14
use App\Models\Bot;
15
use App\Services\DataProviders\DataProviderInterface;
16
use App\Services\DataProviders\ExternalArticle;
17
use App\Services\DataProviders\Manager;
18
use Carbon\Carbon;
19
use Illuminate\Console\Command;
20
21
/**
22
 * Class DataProvidersImport.
23
 */
24
class DataProvidersImport extends Command
25
{
26
    /**
27
     * The name and signature of the console command.
28
     *
29
     * @var string
30
     */
31
    protected $signature = 'data-providers:import';
32
33
    /**
34
     * The console command description.
35
     *
36
     * @var string
37
     */
38
    protected $description = 'Run import articles from external services';
39
40
    /**
41
     * @var array
42
     */
43
    private $published = [];
44
45
    /**
46
     * @param Manager $manager
47
     * @throws \InvalidArgumentException
48
     */
49
    public function handle(Manager $manager)
50
    {
51
        $a = Article::publishedByBot()
0 ignored issues
show
Bug introduced by
The method publishedByBot() does not exist on App\Models\Article. Did you maybe mean scopePublishedByBot()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
52
            ->with('user')
53
            ->latest('published_at')
54
            ->first();
55
56
        $articles = (array)($a ?? []);
57
58
        /** @var Article $article */
59
        foreach ($articles as $article) {
60
            /** @var Bot $bot */
61
            $bot = $article->user;
62
63
            $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...
64
65
            $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...
66
        }
67
68
        $this->importNotPublished($manager);
69
    }
70
71
    /**
72
     * @param Manager $manager
73
     */
74
    private function importNotPublished(Manager $manager)
75
    {
76
        foreach ($manager as $alias => $provider) {
77
            if (in_array($alias, $this->published, true)) {
78
                continue;
79
            }
80
81
            $this->import(Carbon::createFromTimestamp(0), $provider);
82
        }
83
    }
84
85
    private function import(\DateTime $time, DataProviderInterface $provider)
86
    {
87
        /** @var ExternalArticle[] $latest */
88
        $latest = $provider->getLatest($time);
89
90
        foreach ($latest as $external) {
91
            $article = new Article();
92
93
            $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...
94
            $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...
95
96
            $external->fill($article);
97
98
            $article->save();
99
        }
100
    }
101
}
102