55 */
66namespace Magento \TaxSampleData \Model ;
77
8+ use Magento \Framework \App \ObjectManager ;
89use Magento \Framework \Setup \SampleData \Context as SampleDataContext ;
910
1011/**
1112 * Class Tax
13+ *
14+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1215 */
1316class Tax
1417{
@@ -57,6 +60,13 @@ class Tax
5760 */
5861 protected $ csvReader ;
5962
63+ /**
64+ * Region collection factory.
65+ *
66+ * @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
67+ */
68+ private $ regionCollectionFactory ;
69+
6070 /**
6171 * @param SampleDataContext $sampleDataContext
6272 * @param \Magento\Tax\Api\TaxRuleRepositoryInterface $taxRuleRepository
@@ -66,6 +76,7 @@ class Tax
6676 * @param \Magento\Tax\Model\Calculation\RateFactory $taxRateFactory
6777 * @param \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder
6878 * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
79+ * @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
6980 */
7081 public function __construct (
7182 SampleDataContext $ sampleDataContext ,
@@ -75,7 +86,8 @@ public function __construct(
7586 \Magento \Tax \Api \Data \TaxRateInterfaceFactory $ rateFactory ,
7687 \Magento \Tax \Model \Calculation \RateFactory $ taxRateFactory ,
7788 \Magento \Framework \Api \SearchCriteriaBuilder $ criteriaBuilder ,
78- \Magento \Framework \Api \FilterBuilder $ filterBuilder
89+ \Magento \Framework \Api \FilterBuilder $ filterBuilder ,
90+ \Magento \Directory \Model \ResourceModel \Region \CollectionFactory $ regionCollectionFactory = null
7991 ) {
8092 $ this ->fixtureManager = $ sampleDataContext ->getFixtureManager ();
8193 $ this ->csvReader = $ sampleDataContext ->getCsvReader ();
@@ -86,12 +98,28 @@ public function __construct(
8698 $ this ->taxRateFactory = $ taxRateFactory ;
8799 $ this ->criteriaBuilder = $ criteriaBuilder ;
88100 $ this ->filterBuilder = $ filterBuilder ;
101+ $ this ->regionCollectionFactory = $ regionCollectionFactory ?: ObjectManager::getInstance ()->get (
102+ \Magento \Directory \Model \ResourceModel \Region \CollectionFactory::class
103+ );
89104 }
90105
91106 /**
92107 * {@inheritdoc}
93108 */
94109 public function install (array $ fixtures )
110+ {
111+ $ this ->createTaxRates ($ fixtures );
112+ $ this ->createTaxRules ();
113+ }
114+
115+ /**
116+ * Create tax rates.
117+ *
118+ * @param array $fixtures
119+ * @return void
120+ * @throws \Exception if something went wrong while saving the tax rate.
121+ */
122+ private function createTaxRates (array $ fixtures )
95123 {
96124 foreach ($ fixtures as $ fileName ) {
97125 $ fileName = $ this ->fixtureManager ->getFixture ($ fileName );
@@ -111,19 +139,28 @@ public function install(array $fixtures)
111139 continue ;
112140 }
113141 $ taxRate = $ this ->rateFactory ->create ();
142+ $ regionId = $ this ->getRegionId ($ data ['tax_region_name ' ], $ data ['tax_country_id ' ]);
114143 $ taxRate ->setCode ($ data ['code ' ])
115144 ->setTaxCountryId ($ data ['tax_country_id ' ])
116- ->setTaxRegionId ($ data [ ' tax_region_id ' ] )
145+ ->setTaxRegionId ($ regionId )
117146 ->setTaxPostcode ($ data ['tax_postcode ' ])
118147 ->setRate ($ data ['rate ' ]);
119148 $ this ->taxRateRepository ->save ($ taxRate );
120149 }
150+ }
151+ }
121152
122- $ fixtureFile = 'Magento_TaxSampleData::fixtures/tax_rule.csv ' ;
123- $ fixtureFileName = $ this ->fixtureManager ->getFixture ($ fixtureFile );
124- if (!file_exists ($ fileName )) {
125- continue ;
126- }
153+ /**
154+ * Create tax rules.
155+ *
156+ * @return void
157+ * @throws \Exception if something went wrong while saving the tax rule.
158+ */
159+ private function createTaxRules ()
160+ {
161+ $ fixtureFile = 'Magento_TaxSampleData::fixtures/tax_rule.csv ' ;
162+ $ fixtureFileName = $ this ->fixtureManager ->getFixture ($ fixtureFile );
163+ if (file_exists ($ fixtureFileName )) {
127164
128165 $ rows = $ this ->csvReader ->getData ($ fixtureFileName );
129166 $ header = array_shift ($ rows );
@@ -155,6 +192,22 @@ public function install(array $fixtures)
155192 $ this ->taxRuleRepository ->save ($ taxRule );
156193 }
157194 }
195+ }
196+
197+ /**
198+ * Return region Id by code or name.
199+ *
200+ * @param string $region
201+ * @param string $countryId
202+ * @return string|null
203+ */
204+ private function getRegionId ($ region , $ countryId )
205+ {
206+ $ regionCollection = $ this ->regionCollectionFactory ->create ();
207+ $ regionCollection ->addCountryFilter ($ countryId )
208+ ->addRegionCodeOrNameFilter ($ region )
209+ ->setPageSize (1 );
158210
211+ return $ regionCollection ->getFirstItem ()->getId ();
159212 }
160213}
0 commit comments