Completed
Push — master ( 06db8f...7dd0ed )
by Francimar
04:19
created

Model::newProfile()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 8.6186
c 0
b 0
f 0
cc 7
nc 7
nop 2
crap 7
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\EscBema;
9
use Thermal\Profile\EscPOS;
10
use Thermal\Profile\Generic;
11
use Thermal\Profile\Perto;
12
use Thermal\Profile\Profile;
13
14
class Model
15
{
16
    private $profile;
17
18 33
    public function __construct($name)
19
    {
20 33
        if ($name instanceof Profile) {
21 1
            $this->profile = $name;
22
        } else {
23 32
            $data = self::loadCapabilities();
24 32
            if (is_array($name)) {
25 2
                $capabilities = $name;
26 2
                $name = isset($capabilities['model']) ? $capabilities['model'] : 'Unknow';
27
            } else {
28 30
                if (!isset($data['models'][$name])) {
29 1
                    throw new \Exception(sprintf('Printer model "%s" not supported', $name), 404);
30
                }
31 29
                $capabilities = $data['models'][$name];
32
            }
33 31
            list($profile, $capabilities) = self::expandCapabilities($name, $capabilities, $data);
34 31
            $this->profile = self::newProfile($profile, $capabilities);
35
        }
36 31
    }
37
38 32
    private static function expandCapabilities($model, $capabilities, $data)
39
    {
40 32
        $capabilities = \is_array($capabilities) ? $capabilities : ['profile' => $capabilities];
41 32
        $capabilities['model'] = $model;
42
        // fill inherited fields
43 32
        $profile = $capabilities['profile'];
44 32
        while (isset($capabilities['profile'])) {
45 32
            $inherited = $capabilities['profile'];
46 32
            unset($capabilities['profile']);
47 32
            $parent = $data['profiles'][$inherited];
48 32
            $capabilities = \array_merge($parent, $capabilities);
49
        }
50 32
        return [$profile, $capabilities];
51
    }
52
53 31
    private static function newProfile($profile_name, $capabilities)
54
    {
55 31
        if ($profile_name == 'escbema') {
56 14
            return new EscBema($capabilities);
57
        }
58 17
        if ($profile_name == 'elgin') {
59 4
            return new Elgin($capabilities);
60
        }
61 13
        if ($profile_name == 'daruma') {
62 4
            return new Daruma($capabilities);
63
        }
64 9
        if ($profile_name == 'diebold') {
65 1
            return new Diebold($capabilities);
66
        }
67 8
        if ($profile_name == 'generic') {
68 1
            return new Generic($capabilities);
69
        }
70 7
        if ($profile_name == 'perto') {
71 1
            return new Perto($capabilities);
72
        }
73
        // default profile
74 6
        return new EscPOS($capabilities);
75
    }
76
77 33
    private static function loadCapabilities()
78
    {
79 33
        return require(__DIR__ . '/resources/capabilities.php');
80
    }
81
82 1
    public static function getAll()
83
    {
84 1
        $data = self::loadCapabilities();
85 1
        foreach ($data['models'] as $model => $capabilities) {
86 1
            list($profile, $capabilities) = self::expandCapabilities($model, $capabilities, $data);
87 1
            $data['models'][$model] = $capabilities;
88 1
            $data['models'][$model]['profile'] = $profile;
89
        }
90 1
        return $data['models'];
91
    }
92
93 3
    public function getName()
94
    {
95 3
        return $this->profile->getName();
96
    }
97
98
    /**
99
     * @return \Thermal\Profile\Profile
100
     */
101 30
    public function getProfile()
102
    {
103 30
        return $this->profile;
104
    }
105
}
106