Completed
Push — master ( e81671...a22352 )
by André
93:06 queued 73:42
created

CheckURLsCommand::execute()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 3
nop 2
dl 0
loc 32
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Bundle\EzPublishCoreBundle\Command;
8
9
use eZ\Publish\API\Repository\URLService;
10
use eZ\Publish\API\Repository\Values\URL\Query\Criterion;
11
use eZ\Publish\API\Repository\Values\URL\Query\SortClause;
12
use eZ\Publish\API\Repository\Values\URL\URLQuery;
13
use RuntimeException;
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Helper\ProgressBar;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class CheckURLsCommand extends ContainerAwareCommand
21
{
22
    const DEFAULT_ITERATION_COUNT = 50;
23
    const DEFAULT_REPOSITORY_USER = 'admin';
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function configure()
29
    {
30
        $this->setName('ezplatform:check-urls');
31
        $this->setDescription('Checks validity of external URLs');
32
        $this->addOption(
33
            'iteration-count',
34
            'c',
35
            InputOption::VALUE_OPTIONAL,
36
            'Number of urls to be checked in a single iteration, for avoiding using too much memory',
37
            self::DEFAULT_ITERATION_COUNT
38
        );
39
        $this->addOption(
40
            'user',
41
            'u',
42
            InputOption::VALUE_OPTIONAL,
43
            'eZ Platform username (with Role containing at least Content policies: read, versionread, edit, remove, versionremove)',
44
            self::DEFAULT_REPOSITORY_USER
45
        );
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        $repository = $this->getContainer()->get('ezpublish.api.repository');
54
        $repository->getPermissionResolver()->setCurrentUserReference(
55
            $repository->getUserService()->loadUserByLogin($input->getOption('user'))
56
        );
57
58
        $limit = $input->getOption('iteration-count');
59
        if (!is_numeric($limit) || (int)$limit < 1) {
60
            throw new RuntimeException("'--iteration-count' option should be > 0, got '{$limit}'");
61
        }
62
63
        $query = new URLQuery();
64
        $query->filter = new Criterion\VisibleOnly();
65
        $query->sortClauses = [
66
            new SortClause\URL(),
67
        ];
68
        $query->offset = 0;
69
        $query->limit = $limit;
0 ignored issues
show
Documentation Bug introduced by
It seems like $limit can also be of type double or string. However, the property $limit is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
70
71
        $totalCount = $this->getTotalCount(clone $query);
72
73
        $progress = new ProgressBar($output, $totalCount);
74
        $progress->start();
75
        while ($query->offset < $totalCount) {
76
            $this->getUrlHandler()->check($query);
77
78
            $progress->advance(min($limit, $totalCount - $query->offset));
79
            $query->offset += $limit;
80
        }
81
        $progress->finish();
82
    }
83
84
    private function getTotalCount(URLQuery $query)
85
    {
86
        $repository = $this->getContainer()->get('ezpublish.api.repository');
87
        /** @var URLService $urlService */
88
        $urlService = $repository->getURLService();
89
90
        $query->limit = 0;
91
92
        return $urlService->findUrls($query)->totalCount;
93
    }
94
95
    /**
96
     * @return \eZ\Bundle\EzPublishCoreBundle\URLChecker\URLCheckerInterface
97
     */
98
    private function getUrlHandler()
99
    {
100
        return $this->getContainer()->get('ezpublish.url_checker');
101
    }
102
}
103