Passed
Push — master ( 0efe7a...946c43 )
by Roberto
01:43
created

BaseObject::getStaticEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2019 - present
4
 * updown - BaseObject.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 18/2/2019
8
 * MIT license: https://github.com/biscolab/updown-php/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\UpDown\Objects;
12
13
use Biscolab\UpDown\Abstracts\AbstractCollection;
14
use Biscolab\UpDown\Abstracts\AbstractObject;
15
use Biscolab\UpDown\Http\UpDownResponse;
16
use Biscolab\UpDown\UpDown;
17
18
/**
19
 * Class BaseObject
20
 * @package Biscolab\UpDown\BaseObject
21
 */
22
class BaseObject extends AbstractObject
23
{
24
25
    /**
26
     * @var string
27
     */
28
    protected static $endpoint = '';
29
30
    /**
31
     * @var string
32
     */
33
    protected static $collection_type = '';
34
35
    /**
36
     * @var string
37
     */
38
    protected static $key = '';
39
40
    /**
41
     * CrudObject constructor.
42
     *
43
     * @param mixed $args
44
     */
45
    public function __construct($args = [])
46
    {
47
48
        if (!is_array($args)) {
49
            $args_[self::getPrimaryKey()] = $args;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$args_ was never initialized. Although not strictly required by PHP, it is generally a good practice to add $args_ = array(); before regardless.
Loading history...
50
        } else {
51
            $args_ = $args;
52
        }
53
54
        parent::__construct($args_);
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public static function getPrimaryKey(): string
61
    {
62
63
        return static::$key;
64
    }
65
66
    /**
67
     * @return null|AbstractCollection
68
     */
69
    public function all(): AbstractCollection
70
    {
71
72
        $path = static::getEndpoint();
73
74
        $response = $this->updown->get($path);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        /** @scrutinizer ignore-call */ 
75
        $response = $this->updown->get($path);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
76
        if (static::$collection_type) {
77
78
            return $this->getCollection($response);
79
        }
80
81
        return null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return null returns the type null which is incompatible with the type-hinted return Biscolab\UpDown\Abstracts\AbstractCollection.
Loading history...
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    protected static function getEndpoint(): string
88
    {
89
90
        return UpDown::API_URL . static::$endpoint;
91
    }
92
93
    /**
94
     * @param UpDownResponse $response
95
     *
96
     * @return mixed
97
     */
98
    protected function getCollection(UpDownResponse $response): AbstractCollection
99
    {
100
101
        $collection_type = static::$collection_type;
102
103
        return new $collection_type($response->getResult()->getData());
104
    }
105
106
    /**
107
     * @param bool|null $with_id
108
     *
109
     * @return string
110
     */
111
    public function prepareApiPath(?bool $with_id = true): string
112
    {
113
114
        $path = static::getEndpoint();
115
116
        if ($with_id) {
117
            $path .= '/' . $this->getId();
118
        }
119
120
        return $path;
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126
    public function getId()
127
    {
128
129
        return $this->{static::getPrimaryKey()};
130
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getStaticEndpoint(): string
137
    {
138
139
        return static::getEndpoint();
140
    }
141
142
}