Completed
Push — master ( a25093...372383 )
by Iurii
02:06
created

Main::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * @package Country flags
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\country_flag;
11
12
use gplcart\core\Library;
13
14
/**
15
 * Main class for Country flags module
16
 */
17
class Main
18
{
19
20
    /**
21
     * Library class instance
22
     * @var \gplcart\core\Library $library
23
     */
24
    protected $library;
25
26
    /**
27
     * @param Library $library
28
     */
29
    public function __construct(Library $library)
30
    {
31
        $this->library = $library;
32
    }
33
34
    /**
35
     * Implements hook "library.list"
36
     * @param array $libraries
37
     */
38
    public function hookLibraryList(array &$libraries)
39
    {
40
        $libraries['flag-icon-css'] = array(
41
            'name' => /* @text */'Country flags',
42
            'description' => /* @text */'A collection of all country flags in SVG',
43
            'type' => 'asset',
44
            'module' => 'country_flag',
45
            'url' => 'https://github.com/lipis/flag-icon-css',
46
            'download' => 'https://github.com/lipis/flag-icon-css/archive/2.8.0.zip',
47
            'version_source' => array(
48
                'file' => 'vendor/flag-icon-css/package.json'
49
            ),
50
            'files' => array(
51
                'vendor/flag-icon-css/css/flag-icon.min.css'
52
            ),
53
        );
54
    }
55
56
    /**
57
     * Implements hook "country.get.after"
58
     * @param string $code
59
     * @param array $result
60
     */
61
    public function hookCountryGetAfter($code, &$result)
62
    {
63
        $this->setCountryFlag($code, $result);
64
    }
65
66
    /**
67
     * Implements hook "country.list"
68
     * @param array $countries
69
     */
70
    public function hookCountryList(array &$countries)
71
    {
72
        $this->setCountryFlags($countries);
73
    }
74
75
    /**
76
     * Implements hook "module.enable.after"
77
     */
78
    public function hookModuleEnableAfter()
79
    {
80
        $this->library->clearCache();
81
    }
82
83
    /**
84
     * Implements hook "module.disable.after"
85
     */
86
    public function hookModuleDisableAfter()
87
    {
88
        $this->library->clearCache();
89
    }
90
91
    /**
92
     * Implements hook "module.install.after"
93
     */
94
    public function hookModuleInstallAfter()
95
    {
96
        $this->library->clearCache();
97
    }
98
99
    /**
100
     * Implements hook "module.uninstall.after"
101
     */
102
    public function hookModuleUninstallAfter()
103
    {
104
        $this->library->clearCache();
105
    }
106
107
    /**
108
     * Returns an array of country flag images keyed by country code
109
     */
110
    public function getCountryFlags()
111
    {
112
        $list = &gplcart_static(__METHOD__);
113
114
        if (isset($list)) {
115
            return $list;
116
        }
117
118
        $list = array();
119
        foreach (glob(__DIR__ . '/vendor/flag-icon-css/flags/*/*.svg') as $file) {
120
            $info = pathinfo($file);
121
            $list[$info['filename']][basename($info['dirname'])] = $file;
122
        }
123
124
        return $list;
125
    }
126
127
    /**
128
     * Sets a country flag image
129
     * @param string $code
130
     * @param array $country
131
     */
132
    protected function setCountryFlag($code, array &$country)
133
    {
134
        $lower_code = strtolower($code);
135
        $images = $this->getCountryFlags();
136
        if (isset($images[$lower_code]['1x1']) && !isset($country['image'])) {
137
            $country['image'] = $images[$lower_code]['1x1'];
138
        }
139
    }
140
141
    /**
142
     * Sets flags for an array of images
143
     * @param array $countries
144
     */
145
    protected function setCountryFlags(array &$countries)
146
    {
147
        $images = $this->getCountryFlags();
148
        foreach ($countries as $code => &$country) {
149
            $code = strtolower($code);
150
            if (isset($images[$code]['1x1']) && !isset($country['image'])) {
151
                $country['image'] = $images[$code]['1x1'];
152
            }
153
        }
154
    }
155
156
}
157