Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#1151)
by Henrique
03:19
created

VideoUrl::isSupportedService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use Respect\Validation\Exceptions\ComponentException;
17
use function array_keys;
18
use function is_string;
19
use function mb_strtolower;
20
use function preg_match;
21
use function sprintf;
22
23
/**
24
 * Validates if the input is a video URL value.
25
 *
26
 * @author Danilo Correa <[email protected]>
27
 * @author Henrique Moody <[email protected]>
28
 * @author Ricardo Gobbo <[email protected]>
29
 */
30
final class VideoUrl extends AbstractRule
31
{
32
    /**
33
     * @var string
34
     */
35
    private $service;
36
37
    /**
38
     * @var array
39
     */
40
    private const SERVICES = [
41
        'youtube' => '@^https?://(www\.)?(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^\"&?/]{11})@i',
42
        'vimeo' => '@^https?://(www\.)?(player\.)?(vimeo\.com/)((channels/[A-z]+/)|(groups/[A-z]+/videos/)|(video/))?([0-9]+)@i',
43
    ];
44
45
    /**
46
     * Create a new instance VideoUrl.
47
     *
48
     * @param string|null $service
49
     *
50
     * @throws ComponentException when the given service is not supported
51
     */
52 1
    public function __construct(string $service = null)
53
    {
54 1
        if (null !== $service && !$this->isSupportedService($service)) {
55
            throw new ComponentException(sprintf('"%s" is not a recognized video service.', $service));
56
        }
57
58 1
        $this->service = $service;
59 1
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 22
    public function validate($input): bool
65
    {
66 22
        if (!is_string($input)) {
67
            return false;
68
        }
69
70 22
        if (null !== $this->service) {
71 8
            return $this->isValid($this->service, $input);
72
        }
73
74 15
        foreach (array_keys(self::SERVICES) as $service) {
75 15
            if (!$this->isValid($service, $input)) {
76 12
                continue;
77
            }
78
79 6
            return true;
80
        }
81
82 10
        return false;
83
    }
84
85 1
    private function isSupportedService(string $service): bool
86
    {
87 1
        return isset(self::SERVICES[mb_strtolower($service)]);
88
    }
89
90 22
    private function isValid(string $service, string $input): bool
91
    {
92 22
        return preg_match(self::SERVICES[mb_strtolower($service)], $input) > 0;
93
    }
94
}
95