DriverManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Bedard\Shop\Classes;
2
3
use System\Classes\PluginManager;
4
5
class DriverManager
6
{
7
    /**
8
     * @var \System\Classes\PluginManager Manager
9
     */
10
    protected $manager;
11
12
    /**
13
     * Construct.
14
     *
15
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
16
     */
17
    public function __construct()
18
    {
19
        $this->manager = PluginManager::instance();
20
    }
21
22
    /**
23
     * Get all drivers.
24
     *
25
     * @return array
26
     */
27
    public function getDrivers()
28
    {
29
        $drivers = [];
30
31
        foreach ($this->manager->getPlugins() as $id => $plugin) {
32
            if (! method_exists($plugin, 'registerShopDrivers')) {
33
                continue;
34
            }
35
36
            foreach ($plugin->registerShopDrivers() as $driver) {
37
                if (array_key_exists('name', $driver)) {
38
                    $driver['name'] = trans($driver['name']);
39
                }
40
41
                $drivers[] = $driver;
42
            }
43
        }
44
45
        return $drivers;
46
    }
47
48
    /**
49
     * Get a type of driver.
50
     *
51
     * @param  string   $type
52
     * @return array
53
     */
54
    public function getDriversByType($type)
55
    {
56
        return array_filter($this->getDrivers(), function ($driver) use ($type) {
57
            return $driver['type'] === $type;
58
        });
59
    }
60
61
    /**
62
     * Get the registration of a particular driver.
63
     *
64
     * @param  string       $class
65
     * @return array|null
66
     */
67
    public function getRegistration($class)
68
    {
69
        foreach ($this->getDrivers() as $driver) {
70
            if ($driver['class'] === $class) {
71
                return $driver;
72
            }
73
        }
74
75
        return null;
76
    }
77
}
78