Test Setup Failed
Push — master ( b9c0be...6a3b1f )
by Francimar
06:58
created

Model::newProfile()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 24
cts 24
cp 1
rs 6.9666
c 0
b 0
f 0
cc 12
nc 12
nop 2
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Thermal;
4
5
use Thermal\Profile\Daruma;
6
use Thermal\Profile\Diebold;
7
use Thermal\Profile\Elgin;
8
use Thermal\Profile\Bematech;
9
use Thermal\Profile\Epson;
10
use Thermal\Profile\Generic;
11
use Thermal\Profile\ControliD;
12
use Thermal\Profile\Perto;
13
use Thermal\Profile\Profile;
14
use Thermal\Profile\Dataregis;
15
use Thermal\Profile\Sweda;
16
17
class Model
18
{
19
    /**
20
     * Model profile
21
     *
22
     * @var \Thermal\Profile\Profile
23
     */
24
    private $profile;
25
26 53
    public function __construct($name)
27
    {
28 53
        if ($name instanceof Profile) {
29 1
            $this->profile = $name;
30
        } else {
31 52
            $data = self::loadCapabilities();
32 52
            if (is_array($name)) {
33 3
                $capabilities = $name;
34 3
                $name = isset($capabilities['model']) ? $capabilities['model'] : 'Unknow';
35
            } else {
36 51
                if (!isset($data['models'][$name])) {
37 1
                    throw new \Exception(sprintf('Printer model "%s" not supported', $name), 404);
38
                }
39 50
                $capabilities = $data['models'][$name];
40
            }
41 51
            list($profile, $capabilities) = self::expandCapabilities($name, $capabilities, $data);
42 51
            $this->profile = self::newProfile($profile, $capabilities);
43
        }
44 52
    }
45
46 52
    private static function expandCapabilities($model, $capabilities, $data)
47
    {
48 52
        $capabilities = \is_array($capabilities) ? $capabilities : ['profile' => $capabilities];
49 52
        $capabilities['model'] = $model;
50
        // fill inherited fields
51 52
        $profile = $capabilities['profile'];
52 52
        while (isset($capabilities['profile'])
53 52
            && isset($data['profiles'][$capabilities['profile']])
54
        ) {
55 52
            $inherited = $capabilities['profile'];
56 52
            unset($capabilities['profile']);
57 52
            $parent = $data['profiles'][$inherited];
58 52
            $capabilities = \array_merge($parent, $capabilities);
59
        }
60 52
        return [$profile, $capabilities];
61
    }
62
63
    /**
64
     * Instantiate new profile from name
65
     *
66
     * @param string $profile_name
67
     * @param array $capabilities
68
     * @return \Thermal\Profile\Profile
69
     */
70 51
    private static function newProfile($profile_name, $capabilities)
71
    {
72 51
        switch ($profile_name) {
73 51
            case 'bematech':
74 16
                return new Bematech($capabilities);
75
76 35
            case 'epson':
77 34
            case 'tmt20':
78 13
                return new Epson($capabilities);
79
80 23
            case 'elgin':
81 5
                return new Elgin($capabilities);
82
83 18
            case 'daruma':
84 7
                return new Daruma($capabilities);
85
86 11
            case 'diebold':
87 2
                return new Diebold($capabilities);
88
89 9
            case 'sweda':
90 1
                return new Sweda($capabilities);
91
92 8
            case 'dataregis':
93 1
                return new Dataregis($capabilities);
94
95 7
            case 'controlid':
96 2
                return new ControliD($capabilities);
97
98 5
            case 'perto':
99 2
                return new Perto($capabilities);
100
101 3
            case 'generic':
102 2
                return new Generic($capabilities);
103
104
            default:
105 1
                return new Epson($capabilities);
106
        }
107
108
    }
109
110 53
    private static function loadCapabilities()
111
    {
112 53
        return require(__DIR__ . '/resources/capabilities.php');
113
    }
114
115 1
    public static function getAll()
116
    {
117 1
        $data = self::loadCapabilities();
118 1
        foreach ($data['models'] as $model => $capabilities) {
119 1
            list($profile, $capabilities) = self::expandCapabilities($model, $capabilities, $data);
120 1
            $data['models'][$model] = $capabilities;
121 1
            $data['models'][$model]['profile'] = $profile;
122
        }
123 1
        return $data['models'];
124
    }
125
126 4
    public function getName()
127
    {
128 4
        return $this->profile->getName();
129
    }
130
131
    /**
132
     * @return \Thermal\Profile\Profile
133
     */
134 51
    public function getProfile()
135
    {
136 51
        return $this->profile;
137
    }
138
}
139