Completed
Push — master ( 1e1a94...fe204c )
by Valentyn
05:23
created

LoadWatchCards   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 9
dl 0
loc 69
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A configure() 0 7 1
A execute() 0 33 4
1
<?php
2
3
namespace App\Movies\Command;
4
5
use App\Movies\Entity\Movie;
6
use App\Movies\Entity\MovieCard;
7
use App\Movies\Parser\Megogo;
8
use App\Movies\Repository\MovieCardRepository;
9
use App\Movies\Repository\MovieRepository;
10
use App\Users\Repository\UserRepository;
11
use Doctrine\ORM\EntityManagerInterface;
12
use Psr\SimpleCache\CacheInterface;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class LoadWatchCards extends Command
18
{
19
    /** @var $repository MovieRepository */
20
    private $repository;
21
22
    private $userRepository;
23
24
    /** @var $parser Megogo */
25
    private $parser;
26
27
    private $em;
28
    private $cache;
29
30
    private const CACHE_TIME = 2592000; // 30 days
31
32
    public function __construct(EntityManagerInterface $em, UserRepository $userRepository, MovieCardRepository $cardRepository, MovieRepository $repository, Megogo $parser, CacheInterface $cache, ?string $name = null)
33
    {
34
        parent::__construct($name);
35
36
        $this->repository = $repository;
37
        $this->cardRepository = $cardRepository;
0 ignored issues
show
Bug introduced by
The property cardRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
38
        $this->userRepository = $userRepository;
39
        $this->parser = $parser;
40
        $this->cache = $cache;
41
        $this->em = $em;
42
    }
43
44
    protected function configure()
45
    {
46
        $this
47
            ->setName('app:load-watch-cards')
48
            ->setDescription('Find movies on megogo')
49
            ->setHelp('php bin/console app:load-watch-cards');
50
    }
51
52
    protected function execute(InputInterface $i, OutputInterface $o)
53
    {
54
        $title = 'Megogo';
55
        $locale = 'uk';
56
        $movies = $this->repository->findAllWithoutCardsQuery($title, $locale)->getResult();
57
        $user = $this->userRepository->findOneBy([
58
            'username' => 'support'
59
        ]);
60
61
        /** @var $movie Movie */
62
        foreach ($movies as $movie) {
63
            $cacheKey = 'megogo_notfound_' . $movie->getId();
64
            if ($this->cache->has($cacheKey)) {
65
                continue;
66
            }
67
68
            try {
69
                $url = $this->parser->getUrlByTitle($movie->getOriginalTitle());
70
            } catch (\ErrorException $e) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
71
                $this->cache->set($cacheKey, true, self::CACHE_TIME);
72
                $o->writeln(sprintf(
73
                    '[LoadWatchCards] [%s]: %s', $movie->getOriginalTitle(), $e->getMessage()
74
                ));
75
                continue;
76
            }
77
78
            $description = sprintf('Дивитись онлайн в хорошій якості у найбільшому онлайн кінотеатрі у східній Європі');
79
            $card = new MovieCard($movie, $user, $locale, $title, $description, MovieCard::TYPE_WATCH, $url);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userRepository->f...sername' => 'support')) on line 57 can be null; however, App\Movies\Entity\MovieCard::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
80
81
            $this->em->persist($card);
82
            $this->em->flush();
83
        }
84
    }
85
}