Indexable::maxIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TheIconic\Tracking\GoogleAnalytics\Traits;
4
5
use TheIconic\Tracking\GoogleAnalytics\Exception\InvalidIndexException;
6
use TheIconic\Tracking\GoogleAnalytics\Exception\InvalidNameException;
7
8
/**
9
 * Class Indexable
10
 *
11
 * Contains logic for indexing a parameter string with a valid placeholder.
12
 *
13
 * @package TheIconic\Tracking\GoogleAnalytics\Traits
14
 */
15
trait Indexable
16
{
17
    /**
18
     * Placeholder for the index.
19
     */
20
    private $indexPlaceholder = ':i:';
21
22
    /**
23
     * Minimum value index can take in GA.
24
     *
25
     * @return int
26
     */
27
    protected function minIndex()
28
    {
29
        return 1;
30
    }
31
32
    /**
33
     * Maximum value index can take in GA.
34
     *
35
     * @return int
36
     */
37
    protected function maxIndex()
38
    {
39
        return 200;
40
    }
41
42
    /**
43
     * Replaces a placeholder for the index passed.
44
     *
45
     * @param string $string
46
     * @param int $index
47
     * @return string
48
     * @throws \TheIconic\Tracking\GoogleAnalytics\Exception\InvalidIndexException
49
     * @throws \TheIconic\Tracking\GoogleAnalytics\Exception\InvalidNameException
50
     */
51
    protected function addIndex($string, $index)
52
    {
53
        if (empty($string)) {
54
            throw new InvalidNameException('Name attribute not defined for class ' . get_class($this));
55
        }
56
57
        if (strpos($string, $this->indexPlaceholder) !== false) {
58
            if (!is_numeric($index) || $index < $this->minIndex() || $index > $this->maxIndex()) {
0 ignored issues
show
introduced by
The condition is_numeric($index) is always true.
Loading history...
59
                throw new InvalidIndexException(
60
                    'When setting parameter ' . get_class($this)
61
                    . ' a numeric index between 1 - 200 must be passed for the second argument'
62
                );
63
            }
64
        }
65
66
        return str_replace($this->indexPlaceholder, $index, $string);
67
    }
68
}
69