Completed
Push — master ( 071f6e...2ed909 )
by Seth
25:30
created

AbstractCanvasSearchDomain   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 80
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 2
A setApi() 0 7 2
A getApi() 0 4 1
A setId() 0 9 3
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
    const ID = 'id';
17
    const API = 'api';
18
19
    /**
20
     * API access object
21
     * @var CanvasPest
22
     */
23
    protected $api;
24
25
    /**
26
     * Construct a CanvasSearchDomain from `$params`, requires `id` and `api`
27
     * params, will extract `url` param from `api`, if necessary.
28
     *
29
     * @inheritdoc
30
     *
31
     * @param mixed[string] $params
1 ignored issue
show
Documentation introduced by
The doc-type mixed[string] could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
32
     */
33
    public function __construct($params)
34
    {
35
        static::requireParameter($params, self::ID);
36
        static::requireParameter($params, self::API, CanvasPest::class);
37
38
        static::defaultParameter(
39
            $params,
40
            'icon',
41
            'https://du11hjcvx0uqb.cloudfront.net/dist/images/favicon-e10d657a73.ico'
42
        );
43
44
        $this->setApi($params[self::API]);
45
        if (empty($params[self::URL])) {
46
            $params[self::URL] = preg_replace('%^(.*)/api/v\d+$%', '$1', $this->getApi()->base_url);
47
        }
48
49
        parent::__construct($params);
50
51
        $this->setId($params[self::ID]);
52
    }
53
54
    /**
55
     * Update the `$api` field
56
     *
57
     * @param CanvasPest $api
58
     * @throws Exception If `$api` is `NULL`
59
     */
60
    protected function setApi(CanvasPest $api)
61
    {
62
        if ($api === null) {
63
            throw new Exception('Initialized CanvasPest object required');
64
        }
65
        $this->api = $api;
66
    }
67
68
    /**
69
     * Get the Canvas API field
70
     *
71
     * @return CanvasPest
72
     */
73
    protected function getApi()
74
    {
75
        return $this->api;
76
    }
77
78
    /**
79
     * Update the ID of the Canvas object
80
     *
81
     * @used-by AbstractCanvasSearchDomain::__construct()
82
     * @param string|integer $id Canvas ID or SIS ID formatted as `sis_*_id:*`
83
     */
84
    protected function setId($id)
85
    {
86
        if (!is_numeric($id) &&
87
            !preg_match('/^sis_[a-z]+_id:\S+$/i', $id)
88
        ) {
89
            throw new Exception('ID must be a Canvas ID or SIS ID, received:' . PHP_EOL . print_r($id, true));
90
        }
91
        $this->id = $id;
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
92
    }
93
}
94