dynamic /
silverstripe-foxy
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Dynamic\Foxy\Task; |
||
| 4 | |||
| 5 | use Dynamic\Foxy\Model\FoxyHelper; |
||
| 6 | use Dynamic\Foxy\Model\OptionType; |
||
|
0 ignored issues
–
show
|
|||
| 7 | use Dynamic\Foxy\Model\Variation; |
||
| 8 | use Dynamic\Foxy\Model\VariationType; |
||
| 9 | use SilverStripe\Control\HTTPRequest; |
||
| 10 | use SilverStripe\Dev\BuildTask; |
||
| 11 | use SilverStripe\ORM\ValidationException; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Class OptionToVariationMigration |
||
| 15 | * @package Dynamic\Foxy\Task |
||
| 16 | */ |
||
| 17 | class OptionToVariationMigration extends BuildTask |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $title = 'Foxy - Option to Variation Migration'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private static $segment = 'foxy-option-to-variation-migration'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $variation_fields = [ |
||
| 33 | 'Title', |
||
| 34 | 'WeightModifier', |
||
| 35 | 'CodeModifier', |
||
| 36 | 'PriceModifier', |
||
| 37 | 'WeightModifierAction', |
||
| 38 | 'CodeModifierAction', |
||
| 39 | 'PriceModifierAction', |
||
| 40 | 'Available', |
||
| 41 | 'OptionModifierKey', |
||
| 42 | 'SortOrder', |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | private $type_map = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param HTTPRequest $request |
||
| 52 | * @throws ValidationException |
||
| 53 | */ |
||
| 54 | public function run($request) |
||
| 55 | { |
||
| 56 | foreach ($this->yieldSingle(FoxyHelper::singleton()->getProducts()) as $product) { |
||
| 57 | foreach ($this->yieldSingle($product->Options()) as $option) { |
||
| 58 | $this->createProductVariation($product, $option); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return \Generator |
||
| 65 | */ |
||
| 66 | protected function yieldSingle($list) |
||
| 67 | { |
||
| 68 | foreach ($list as $item) { |
||
| 69 | yield $item; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param $product |
||
| 75 | * @param $option |
||
| 76 | * @return int |
||
| 77 | * @throws ValidationException |
||
| 78 | */ |
||
| 79 | protected function createProductVariation($product, $option) |
||
| 80 | { |
||
| 81 | $variation = Variation::create(); |
||
| 82 | |||
| 83 | foreach ($this->yieldSingle($this->variation_fields) as $fieldName) { |
||
| 84 | $variation->{$fieldName} = $option->{$fieldName}; |
||
| 85 | } |
||
| 86 | |||
| 87 | $variation->VariationTypeID = $this->findOrMakeVariationType($option); |
||
| 88 | $variation->ProductID = $product->ID; |
||
| 89 | |||
| 90 | return $variation->write(); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param $option |
||
| 95 | * @return mixed |
||
| 96 | * @throws ValidationException |
||
| 97 | */ |
||
| 98 | protected function findOrMakeVariationType($option) |
||
| 99 | { |
||
| 100 | if (!array_key_exists($option->Type, $this->type_map)) { |
||
| 101 | $optionType = OptionType::get()->byID($option->Type); |
||
| 102 | $variationType = VariationType::create(); |
||
| 103 | $variationType->Title = $optionType->Title; |
||
| 104 | $variationType->write(); |
||
| 105 | |||
| 106 | $this->assignKey($option->Type, $variationType->ID); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $this->type_map[$option->Type]; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param $optionTypeID |
||
| 114 | * @param $variationTypeID |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | protected function assignKey($optionTypeID, $variationTypeID) |
||
| 118 | { |
||
| 119 | $this->type_map[$optionTypeID] = $variationTypeID; |
||
| 120 | |||
| 121 | return $this; |
||
| 122 | } |
||
| 123 | } |
||
| 124 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths