GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#45)
by
unknown
04:35
created

DomainWhitelisting::validateDomains()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Tgallice\FBMessenger\Model\ThreadSetting;
4
5
use Tgallice\FBMessenger\Model\ThreadSetting;
6
7
class DomainWhitelisting implements ThreadSetting, \JsonSerializable
8
{
9
    const TYPE_ADD = 'add';
10
    const TYPE_REMOVE = 'remove';
11
    
12
    const WHITELISTED_DOMAINS = 'whitelisted_domains';
13
    const DOMAIN_ACTION_TYPE = 'domain_action_type';
14
    
15
    /**
16
     * @var string
17
     */
18
    private $action;
19
    
20
    /**
21
     * @var array
22
     */
23
    private $domains = [];
24
    
25
    /**
26
     * 
27
     * @param string $action
28
     * @param array $domains
29
     * 
30
     * @throws \InvalidArgumentException
31
     */
32 9
    public function __construct($domains, $action = DomainWhitelisting::TYPE_ADD)
33
    {
34 9
        self::validateAction($action);
35 8
        self::validateDomains($domains);
36
        
37 6
        $this->action = $action;
38 6
        $this->domains = $domains;
39 6
    }
40
    
41
    /**
42
     * 
43
     * @return array
44
     */
45 1
    public function getDomains()
46
    {
47 1
        return $this->domains;
48
    }
49
    
50
    /**
51
     * 
52
     * @return string
53
     */
54 2
    public function getAction()
55
    {
56 2
        return $this->action;
57
    }
58
    
59
    /**
60
     * 
61
     * @param string $action
62
     * 
63
     * @throws \InvalidArgumentException
64
     */
65 9
    public static function validateAction($action)
66
    {
67 9
        if(!in_array($action, [DomainWhitelisting::TYPE_ADD, DomainWhitelisting::TYPE_REMOVE])) {
68 1
            throw new \InvalidArgumentException('The action must be type: "add" or "remove".');
69
        }
70 8
    }
71
    
72
    /**
73
     * 
74
     * @param array $domains
75
     * 
76
     * @throws \InvalidArgumentException
77
     */
78 8
    public static function validateDomains($domains)
79
    {
80 8
        if(!is_array($domains)) {
81 1
            throw new \InvalidArgumentException('Domains must be a array.');
82
        }
83
        
84
        //https://developers.facebook.com/docs/messenger-platform/thread-settings/domain-whitelisting
85 7
        foreach($domains as $domain) {
86 7
            if(!preg_match('#^https:\/\/#', $domain)) {
87 1
                throw new \InvalidArgumentException('Each domain must be a "https" protocol.');
88
            }
89 7
        }
90 6
    }
91
    
92
    /**
93
     * @inheritdoc
94
     */
95 2
    public function jsonSerialize()
96
    {
97
        return [
98 2
            DomainWhitelisting::WHITELISTED_DOMAINS => $this->domains,
99 2
            DomainWhitelisting::DOMAIN_ACTION_TYPE => $this->action
100 2
        ];
101
    }
102
}
103