Passed
Push — master ( b808b9...919917 )
by Ehsan
04:54
created

ClassUtility::extractClassNameFromFullName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Botonomous\utility;
4
5
use Botonomous\AbstractBaseSlack;
6
7
/**
8
 * Class ClassUtility.
9
 */
10
class ClassUtility
11
{
12
    /**
13
     * @param string $fullName
14
     *
15
     * @return mixed
16
     */
17 19
    public function extractClassNameFromFullName($fullName)
18
    {
19
        // get last part of the namespace
20 19
        $classParts = explode('\\', $fullName);
21
22 19
        return end($classParts);
23
    }
24
25
    /**
26
     * @param AbstractBaseSlack $object
27
     * @param                   $attributes
28
     *
29
     * @return AbstractBaseSlack
30
     */
31 8
    public function loadAttributes(AbstractBaseSlack $object, $attributes)
32
    {
33
        // if $attributes is not array convert it to array
34 8
        if (!is_array($attributes)) {
35 1
            $attributes = json_decode($attributes, true);
36
        }
37
38 8
        foreach ($attributes as $attributeKey => $attributeValue) {
39 8
            $method = $this->getMethodByAttribute($attributeKey);
40
41
            // ignore if setter function does not exist
42 8
            if (!method_exists($object, $method)) {
43 2
                continue;
44
            }
45
46 8
            $object->$method($attributeValue);
47
        }
48
49 8
        return $object;
50
    }
51
52
    /**
53
     * @param $attributeKey
54
     *
55
     * @return string
56
     */
57 8
    private function getMethodByAttribute($attributeKey)
58
    {
59
        // For id, we cannot use 'set'.$stringUtility->snakeCaseToCamelCase($attributeKey) since it's named slackId
60 8
        if ($attributeKey === 'id') {
61 7
            return 'setSlackId';
62
        }
63
64
        // handle ts because there is setTimestamp instead of setTs
65 8
        $stringUtility = new StringUtility();
66 8
        if ($attributeKey === 'ts' || $stringUtility->endsWith($attributeKey, '_ts')) {
67
            // replace the last ts with timestamp
68 3
            $attributeKey = preg_replace('/ts$/', 'timestamp', $attributeKey);
69
        }
70
71 8
        $method = 'set'.$stringUtility->snakeCaseToCamelCase($attributeKey);
72
73 8
        return $method;
74
    }
75
}
76