SitesTrait::addSite()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: siim
5
 * Date: 18.02.19
6
 * Time: 9:06
7
 */
8
9
namespace Sf4\Api\RequestHandler\Traits;
10
11
use Sf4\Api\DependencyInjection\Configuration;
12
13
trait SitesTrait
14
{
15
    /** @var array|null $sites */
16
    protected $sites = [];
17
18
    /**
19
     * @return array|null
20
     */
21
    public function getSites(): ?array
22
    {
23
        return $this->sites;
24
    }
25
26
    /**
27
     * @param array|null $sites
28
     */
29
    public function setSites(?array $sites): void
30
    {
31
        $this->sites = $sites;
32
    }
33
34
    /**
35
     * @param array|null $sites
36
     */
37
    public function addSites(?array $sites): void
38
    {
39
        if (!$sites) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sites of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
            return ;
41
        }
42
43
        foreach ($sites as $site) {
44
            $this->addSite($site);
45
        }
46
    }
47
48
    /**
49
     * @param array $site
50
     */
51
    public function addSite(array $site): void
52
    {
53
        if (isset(
54
            $site[Configuration::SITES_SITE],
55
            $site[Configuration::SITES_URL],
56
            $site[Configuration::SITES_TOKEN]
57
        )) {
58
            foreach ($this->sites as $thisSite) {
59
                if ($thisSite[Configuration::SITES_SITE] === $site[Configuration::SITES_SITE]) {
60
                    return ;
61
                }
62
            }
63
            $this->sites[] = $site;
64
        }
65
    }
66
}
67