6
6
7
7
namespace Magento \Directory \Setup ;
8
8
9
- use Magento \Directory \Helper \Data ;
10
9
use Magento \Framework \App \ResourceConnection ;
11
10
use Magento \Framework \DB \Adapter \AdapterInterface ;
12
11
13
12
/**
14
- * Add Required Regions for Country
13
+ * Class DataInstaller
14
+ * @package Magento\Directory\Setup
15
15
*/
16
16
class DataInstaller
17
17
{
18
+ /**
19
+ * @var \Magento\Directory\Helper\Data
20
+ */
21
+ private $ data ;
22
+
18
23
/**
19
24
* @var ResourceConnection
20
25
*/
21
26
private $ resourceConnection ;
22
27
23
28
/**
24
29
* DatInstaller constructor.
25
- *
30
+ * @param \Magento\Directory\Helper\Data $data
26
31
* @param ResourceConnection $resourceConnection
27
32
*/
28
33
public function __construct (
34
+ \Magento \Directory \Helper \Data $ data ,
29
35
ResourceConnection $ resourceConnection
30
36
) {
37
+ $ this ->data = $ data ;
31
38
$ this ->resourceConnection = $ resourceConnection ;
32
39
}
33
40
34
41
/**
35
42
* Add country-region data.
36
43
*
37
- * @param AdapterInterface $adapter
38
- * @param array $data
39
- * @return void
44
+ * @param AdapterInterface $adapter
45
+ * @param array $data
40
46
*/
41
- public function addCountryRegions (AdapterInterface $ adapter , array $ data ): void
47
+ public function addCountryRegions (AdapterInterface $ adapter , array $ data )
42
48
{
43
- $ where = [
44
- $ adapter ->quoteInto ('path = ? ' , Data::XML_PATH_STATES_REQUIRED ),
45
- $ adapter ->quoteInto ('scope = ? ' , 'default ' ),
46
- $ adapter ->quoteInto ('scope_id = ? ' , 0 ),
47
- ];
48
-
49
- $ select = $ adapter ->select ()
50
- ->from ($ this ->resourceConnection ->getTableName ('core_config_data ' ), 'value ' )
51
- ->where (implode (' AND ' , $ where ));
52
-
53
- $ currRequiredStates = $ adapter ->fetchOne ($ select );
54
- $ currRequiredStates = (!empty ($ currRequiredStates )) ? explode (', ' , $ currRequiredStates ) : [];
55
-
56
49
/**
57
50
* Fill table directory/country_region
58
51
* Fill table directory/country_region_name for en_US locale
@@ -63,21 +56,21 @@ public function addCountryRegions(AdapterInterface $adapter, array $data): void
63
56
$ regionId = $ adapter ->lastInsertId ($ this ->resourceConnection ->getTableName ('directory_country_region ' ));
64
57
$ bind = ['locale ' => 'en_US ' , 'region_id ' => $ regionId , 'name ' => $ row [2 ]];
65
58
$ adapter ->insert ($ this ->resourceConnection ->getTableName ('directory_country_region_name ' ), $ bind );
66
-
67
- if (!in_array ($ row [0 ], $ currRequiredStates )) {
68
- $ currRequiredStates [] = $ row [0 ];
69
- }
70
59
}
71
-
72
60
/**
73
61
* Upgrade core_config_data general/region/state_required field.
74
62
*/
63
+ $ countries = $ this ->data ->getCountryCollection ()->getCountriesWithRequiredStates ();
75
64
$ adapter ->update (
76
65
$ this ->resourceConnection ->getTableName ('core_config_data ' ),
77
66
[
78
- 'value ' => implode (', ' , $ currRequiredStates )
67
+ 'value ' => implode (', ' , array_keys ( $ countries ) )
79
68
],
80
- $ where
69
+ [
70
+ 'scope="default" ' ,
71
+ 'scope_id=0 ' ,
72
+ 'path=? ' => \Magento \Directory \Helper \Data::XML_PATH_STATES_REQUIRED
73
+ ]
81
74
);
82
75
}
83
76
}
0 commit comments