Completed
Push — master ( e9be36...9488e8 )
by Camilo
04:57
created

TelegramTypes::populateObject()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
ccs 18
cts 18
cp 1
rs 8.439
cc 6
eloc 20
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\TelegramAPI\Abstracts;
6
7
use Psr\Log\LoggerInterface;
8
use unreal4u\TelegramAPI\InternalFunctionality\DummyLogger;
9
10
abstract class TelegramTypes
11
{
12
    protected $logger = null;
13
14 17
    public function __construct(array $data = null, LoggerInterface $logger = null)
15
    {
16 17
        if (is_null($logger)) {
17 4
            $logger = new DummyLogger();
18
        }
19
20 17
        $this->logger = $logger;
21 17
        $this->populateObject($data);
22 17
    }
23
24
    /**
25
     * Fills the class with the data passed on through the constructor
26
     *
27
     * @param array $data
28
     * @return TelegramTypes
29
     */
30 17
    final protected function populateObject(array $data = null): TelegramTypes
31
    {
32 17
        if (!is_null($data)) {
33 16
            $this->logger->debug('Detected incoming data on object, foreaching the general data', [
34 16
                'object' => get_class($this)
35
            ]);
36 16
            foreach ($data as $key => $value) {
37 16
                $candidateKey = null;
38 16
                if (is_array($value)) {
39 14
                    $this->logger->debug('Array detected, mapping subobjects for key', ['key' => $key]);
40 14
                    $candidateKey = $this->mapSubObjects($key, $value);
41
                }
42
43 16
                if (!empty($candidateKey)) {
44 14
                    if ($candidateKey instanceof CustomType) {
45 4
                        $this->logger->debug('Done with mapping, injecting custom data type to class', ['key' => $key]);
46 4
                        $this->$key = $candidateKey->data;
0 ignored issues
show
Bug introduced by
The property data does not seem to exist in unreal4u\TelegramAPI\Abstracts\CustomType.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47
                    } else {
48 11
                        $this->logger->debug('Done with mapping, injecting native data type to class', ['key' => $key]);
49 14
                        $this->$key = $candidateKey;
50
                    }
51
                } else {
52 16
                    $this->logger->debug('Performing direct assign for key', ['key' => $key]);
53 16
                    $this->$key = $value;
54
                }
55
            }
56
        }
57
58 17
        return $this;
59
    }
60
61
    /**
62
     * The default is that we have no subobjects at all, so this function will return nothing
63
     *
64
     * @param string $key
65
     * @param array $data
66
     *
67
     * @return TelegramTypes
68
     */
69
    protected function mapSubObjects(string $key, array $data): TelegramTypes
70
    {
71
        return null;
72
    }
73
}
74