Passed
Push — master ( 04e5f2...58629d )
by Leonardo
01:35
created

Model::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LeoCarmo\TelegramBot\Model;
4
5
use LeoCarmo\TelegramBot\Exceptions\RequiredParameterException;
6
use LeoCarmo\TelegramBot\Traits\Fillable;
7
use LeoCarmo\TelegramBot\Traits\Request;
8
9
abstract class Model
10
{
11
12
    use Fillable;
13
14
    use Request;
0 ignored issues
show
introduced by
The trait LeoCarmo\TelegramBot\Traits\Request requires some properties which are not provided by LeoCarmo\TelegramBot\Model\Model: $error_code, $result
Loading history...
15
16
    /**
17
     * @url https://core.telegram.org/bots/api#available-methods
18
     * @var string
19
     */
20
    protected $method;
21
22
    /**
23
     * @var array
24
     */
25
    protected $required;
26
27
    /**
28
     * Fill with parameters
29
     *
30
     * @param array $data
31
     * @return $this
32
     */
33
    public function fill(array $data = [])
34
    {
35
        $this->fillData($data);
36
        return $this;
37
    }
38
39
    /**
40
     * Send action/request
41
     *
42
     * @return array
43
     * @throws RequiredParameterException
44
     */
45
    public function send()
46
    {
47
        $this->checkRequired();
48
49
        return $this->sendRequest(
50
            $this->collectParameters()
51
        );
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    private function collectParameters()
58
    {
59
        $filtered  = array_filter(get_object_vars($this));
60
61
        return array_diff_key($filtered, get_class_vars(self::class));
62
    }
63
64
    /**
65
     * @throws RequiredParameterException
66
     */
67
    private function checkRequired()
68
    {
69
        if (count($this->required)) {
70
71
            foreach ($this->required as $required) {
72
73
                if (! $this->$required) {
74
                    throw new RequiredParameterException($required);
75
                }
76
77
            }
78
79
        }
80
    }
81
82
}