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 (#1143)
by
unknown
03:22
created

VideoUrl   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 7
eloc 18
dl 0
loc 55
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 15 4
A __construct() 0 9 3
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 mb_strtolower;
18
use function preg_match;
19
use function sprintf;
20
21
/**
22
 * Validates if the input is a video URL value:
23
 *
24
 * @author Danilo Correa <[email protected]>
25
 * @author Henrique Moody <[email protected]>
26
 * @author Ricardo Gobbo <[email protected]>
27
 */
28
final class VideoUrl extends AbstractRule
29
{
30
    /**
31
     * @var string
32
     */
33
    public $service;
34
35
    /**
36
     * @var string
37
     */
38
    private $serviceKey;
39
40
    /**
41
     * @var array
42
     */
43
    private $services = [
44
        'youtube' => '@^https?://(www\.)?(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^\"&?/]{11})@i',
45
        'vimeo' => '@^https?://(www\.)?(player\.)?(vimeo\.com/)((channels/[A-z]+/)|(groups/[A-z]+/videos/)|(video/))?([0-9]+)@i',
46
        'twitch' => '@^https?://(((www\.)?twitch\.tv/videos/[0-9]+)|clips\.twitch\.tv/[a-zA-Z]+)$@i',
47
    ];
48
49
    /**
50
     * Create a new instance VideoUrl.
51
     *
52
     * @param string $service
53
     */
54 1
    public function __construct(string $service = null)
55
    {
56 1
        $serviceKey = mb_strtolower((string) $service);
57 1
        if (null !== $service && !isset($this->services[$serviceKey])) {
58
            throw new ComponentException(sprintf('"%s" is not a recognized video service.', $service));
59
        }
60
61 1
        $this->service = $service;
62 1
        $this->serviceKey = $serviceKey;
63 1
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 34
    public function validate($input): bool
69
    {
70 34
        if (isset($this->services[$this->serviceKey])) {
71 13
            return preg_match($this->services[$this->serviceKey], (string) $input) > 0;
72
        }
73
74 22
        foreach ($this->services as $pattern) {
75 22
            if (0 === preg_match($pattern, (string) $input)) {
76 19
                continue;
77
            }
78
79 8
            return true;
80
        }
81
82 15
        return false;
83
    }
84
}
85