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

Passed
Push — master ( 299551...d6699a )
by Emmerson
03:02
created

VideoUrl::isValid()   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 2
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
        'twitch' => '@^https?://(((www\.)?twitch\.tv/videos/[0-9]+)|clips\.twitch\.tv/[a-zA-Z]+)$@i',
44
    ];
45
46
    /**
47
     * Create a new instance VideoUrl.
48
     *
49
     * @param string|null $service
50
     *
51
     * @throws ComponentException when the given service is not supported
52
     */
53 1
    public function __construct(string $service = null)
54
    {
55 1
        if (null !== $service && !$this->isSupportedService($service)) {
56
            throw new ComponentException(sprintf('"%s" is not a recognized video service.', $service));
57
        }
58
59 1
        $this->service = $service;
60 1
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 29
    public function validate($input): bool
66
    {
67 29
        if (!is_string($input)) {
68
            return false;
69
        }
70
71 29
        if (null !== $this->service) {
72 10
            return $this->isValid($this->service, $input);
73
        }
74
75 20
        foreach (array_keys(self::SERVICES) as $service) {
76 20
            if (!$this->isValid($service, $input)) {
77 17
                continue;
78
            }
79
80 6
            return true;
81
        }
82
83 15
        return false;
84
    }
85
86 1
    private function isSupportedService(string $service): bool
87
    {
88 1
        return isset(self::SERVICES[mb_strtolower($service)]);
89
    }
90
91 29
    private function isValid(string $service, string $input): bool
92
    {
93 29
        return preg_match(self::SERVICES[mb_strtolower($service)], $input) > 0;
94
    }
95
}
96