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

URLChecker::fetchUrls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
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\URLChecker;
8
9
use eZ\Publish\API\Repository\URLService as URLServiceInterface;
10
use eZ\Publish\API\Repository\Values\URL\SearchResult;
11
use eZ\Publish\API\Repository\Values\URL\URLQuery;
12
use Psr\Log\LoggerAwareTrait;
13
use Psr\Log\NullLogger;
14
15
class URLChecker implements URLCheckerInterface
16
{
17
    use LoggerAwareTrait;
18
19
    /**
20
     * @var \eZ\Publish\API\Repository\URLService
21
     */
22
    protected $urlService;
23
24
    /**
25
     * @var \eZ\Bundle\EzPublishCoreBundle\URLChecker\URLHandlerRegistryInterface
26
     */
27
    protected $handlerRegistry;
28
29
    /**
30
     * URLChecker constructor.
31
     *
32
     * @param \eZ\Publish\API\Repository\URLService $urlService
33
     * @param \eZ\Bundle\EzPublishCoreBundle\URLChecker\URLHandlerRegistryInterface $handlerRegistry
34
     */
35
    public function __construct(
36
        URLServiceInterface $urlService,
37
        URLHandlerRegistryInterface $handlerRegistry)
38
    {
39
        $this->urlService = $urlService;
40
        $this->handlerRegistry = $handlerRegistry;
41
        $this->logger = new NullLogger();
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function check(URLQuery $query)
48
    {
49
        $grouped = $this->fetchUrls($query);
50
        foreach ($grouped as $scheme => $urls) {
51
            if (!$this->handlerRegistry->supported($scheme)) {
52
                $this->logger->error('Unsupported URL schema: ' . $scheme);
53
                continue;
54
            }
55
56
            $handler = $this->handlerRegistry->getHandler($scheme);
57
            $handler->validate($urls);
58
        }
59
    }
60
61
    /**
62
     * Fetch URLs to check.
63
     *
64
     * @param \eZ\Publish\API\Repository\Values\URL\URLQuery $query
65
     * @return array
66
     */
67
    protected function fetchUrls(URLQuery $query)
68
    {
69
        return $this->groupByScheme(
70
            $this->urlService->findUrls($query)
71
        );
72
    }
73
74
    /**
75
     * Group URLs by schema.
76
     *
77
     * @param \eZ\Publish\API\Repository\Values\URL\SearchResult $urls
78
     * @return array
79
     */
80
    private function groupByScheme(SearchResult $urls)
81
    {
82
        $grouped = [];
83
84
        foreach ($urls as $url) {
85
            $scheme = parse_url($url->url, PHP_URL_SCHEME);
86
            if (!$scheme) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $scheme of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
87
                continue;
88
            }
89
90
            if (!isset($grouped[$scheme])) {
91
                $grouped[$scheme] = [];
92
            }
93
94
            $grouped[$scheme][] = $url;
95
        }
96
97
        return $grouped;
98
    }
99
}
100