Completed
Pull Request — master (#208)
by
unknown
01:47
created

Filters   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 36
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 8 2
A addFilter() 0 5 1
A checkFilterSyntax() 0 6 2
A __toString() 0 4 1
1
<?php
2
3
namespace Spatie\Analytics;
4
5
use Spatie\Analytics\Exceptions\InvalidFilter;
6
7
class Filters
8
{
9
    private $filters;
10
11
    public static function create($filters = []): Filters
12
    {
13
        return new static($filters);
14
    }
15
16
    public function __construct($filters = [])
17
    {
18
        foreach ($filters as $filter) {
19
            $this->checkFilterSyntax($filter);
20
        }
21
22
        $this->filters = $filters;
23
    }
24
25
    public function addFilter($filter)
26
    {
27
        $this->checkFilterSyntax($filter);
28
        $this->filters[] = $filter;
29
    }
30
31
    private function checkFilterSyntax($filter)
32
    {
33
        if (strpos($filter, 'ga:') !== 0) {
34
            throw InvalidFilter::filterMustContainGA($filter);
35
        }
36
    }
37
38
    public function __toString()
39
    {
40
        return implode(';', $this->filters);
41
    }
42
}
43