Completed
Push — master ( a8f20d...0172d7 )
by Seth
01:51
created

AbstractCanvasSearchDomain   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 2
dl 0
loc 95
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A setApi() 0 5 1
A getApi() 0 4 1
A setId() 0 9 2
A getId() 0 4 1
localizeUrl() 0 1 ?
1
<?php
2
3
namespace smtech\StMarksSearch\Canvas;
4
5
use Exception;
6
use smtech\CanvasPest\CanvasPest;
7
use smtech\StMarksSearch\AbstractSearchDomain;
8
9
/**
10
 * Parent class for all Canvas search domains
11
 *
12
 * @author Seth Battis <[email protected]>
13
 */
14
abstract class AbstractCanvasSearchDomain extends AbstractSearchDomain
15
{
16
    /**
17
     * API access object
18
     * @var CanvasPest
19
     */
20
    protected $api;
21
22
    /**
23
     * Canvas ID or SIS ID of the Canvas object
24
     * @var string|integer
25
     */
26
    protected $id;
27
28
    /**
29
     * Construct a CanvasSearchDomain from `$params`, requires `id` param, will
30
     * extract `url` param from `$api` if necessary.
31
     *
32
     * @inheritdoc
33
     *
34
     * @param CanvasPest $api
35
     * @param mixed[] $params
36
     */
37
    public function __construct(CanvasPest $api, $params)
38
    {
39
        /* API access may be required to process $params */
40
        $this->setApi($api);
41
42
        if (empty($params['url'])) {
43
            $params['url'] = preg_replace('%^(.*)/api/v\d+$%', '$1', $this->getApi()->base_url);
44
        }
45
46
        parent::__construct($params);
47
48
        assert(isset($params['id']), new Exception('`id` parameter required'));
49
        $this->setId($params['id']);
50
51
        $this->localizeUrl();
52
    }
53
54
    /**
55
     * Update the `$api` field
56
     *
57
     * @used-by AbstractCanvasSearchDomain::__construct()
58
     * @param CanvasPest $api
59
     */
60
    protected function setApi(CanvasPest $api)
61
    {
62
        assert($api !== null, new Exception('Initialized CanvasPest object required'));
63
        $this->api = $api;
64
    }
65
66
    /**
67
     * Get the Canvas API field
68
     *
69
     * @return CanvasPest
70
     */
71
    protected function getApi()
72
    {
73
        return $this->api;
74
    }
75
76
    /**
77
     * Update the ID of the Canvas object
78
     *
79
     * @used-by AbstractCanvasSearchDomain::__construct()
80
     * @param string|integer $id Canvas ID or SIS ID formatted as `sis_*_id:*`
81
     */
82
    protected function setId($id)
83
    {
84
        assert(
85
            is_numeric($id) ||
86
                preg_match('/^sis_[a-z]+_id:\S+$/i', $id),
87
            new Exception('ID must be a Canvas ID or SIS ID, received:' . PHP_EOL . print_r($id, true))
88
        );
89
        $this->id = $id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $id can also be of type double. However, the property $id is declared as type string|integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
90
    }
91
92
    /**
93
     * Get the Canvas object ID
94
     *
95
     * @return string|integer
96
     */
97
    public function getId()
98
    {
99
        return $this->id;
100
    }
101
102
    /**
103
     * Localize the object URL field within the Canvas instance
104
105
     * @return void
106
     */
107
    abstract protected function localizeUrl();
108
}
109