PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / classes / repository / PriceFormulaSetRepository.php

PriceFormulaSetRepository.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/classes/repository/PriceFormulaSetRepository.php

111 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Description of PriceFormulaSetRepository
5 *
6 * @author Ali2Woo Team
7 */
8
9 namespace AliNext_Lite;;
10
11 class PriceFormulaSetRepository
12 {
13 public const FIELD_PRICE_FORMULA_SETS = 'price_formula_sets';
14
15
16 private PriceFormulaSetFactory $PriceFormulaSetFactory;
17
18 public function __construct(
19 PriceFormulaSetFactory $PriceFormulaSetFactory
20 ) {
21 $this->PriceFormulaSetFactory = $PriceFormulaSetFactory;
22 }
23
24 /**
25 * @return array|PriceFormulaSet[]
26 */
27 public function getAll(): array
28 {
29 $result = [];
30 $data = $this->getAllAsArray();
31
32 foreach ($data as $itemData) {
33 $result[] = $this->PriceFormulaSetFactory->createFormulaSetFromData($itemData);
34 }
35
36 return $result;
37 }
38
39 /**
40 * @throws RepositoryException
41 */
42 public function getOneByName(string $setName): PriceFormulaSet
43 {
44 $data = $this->getAllAsArray();
45
46 foreach ($data as $itemData) {
47 if ($itemData[PriceFormulaSet::NAME_FIELD] === $setName) {
48 return $this->PriceFormulaSetFactory->createFormulaSetFromData($itemData);
49 }
50 }
51
52 throw new RepositoryException(
53 _x( "A price formula set with that name does`t exists. Please choose a different name.",
54 'error text', 'ali2woo')
55 );
56 }
57
58 public function checkExists(string $setName): bool
59 {
60 $data = $this->getAllAsArray();
61
62 foreach ($data as $itemData) {
63 if ($itemData[PriceFormulaSet::NAME_FIELD] === $setName) {
64 return true;
65 }
66 }
67
68 return false;
69 }
70
71 /**
72 * @throws RepositoryException
73 */
74 public function add(PriceFormulaSet $PriceFormulaSet): void
75 {
76 $checkExists = $this->checkExists($PriceFormulaSet->getName());
77
78 if ($checkExists) {
79 throw new RepositoryException(
80 _x( "A price formula set with that name already exists. Please choose a different name.", 'error text', 'ali2woo')
81 );
82 }
83
84 $data = $this->getAllAsArray();
85 $data[] = $PriceFormulaSet->toArray();
86 set_setting(PriceFormulaSetRepository::FIELD_PRICE_FORMULA_SETS, $data);
87 }
88
89 public function removeOneByName(string $setName): void
90 {
91 $data = $this->getAllAsArray();
92
93 $newData = [];
94
95 foreach ($data as $itemData) {
96 if ($itemData[PriceFormulaSet::NAME_FIELD] === $setName) {
97 continue;
98 }
99 $newData[] = $itemData;
100 }
101 set_setting(PriceFormulaSetRepository::FIELD_PRICE_FORMULA_SETS, $newData);
102 }
103
104 private function getAllAsArray(): array
105 {
106 $data = get_setting(PriceFormulaSetRepository::FIELD_PRICE_FORMULA_SETS);
107
108 return $data && is_array($data) ? $data : [];
109 }
110 }
111