Passed
Pull Request — master (#99)
by Nic
02:48
created

OptionToVariationMigration   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 33
c 2
b 0
f 0
dl 0
loc 93
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 5 3
A createProductVariation() 0 12 2
A yieldSingle() 0 4 2
A findOrMakeVariationType() 0 12 2
1
<?php
2
3
namespace Dynamic\Foxy\Task;
4
5
use Dynamic\Foxy\Model\FoxyHelper;
6
use Dynamic\Foxy\Model\OptionType;
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->type_map[$optionType->ID] = $variationType->ID;
107
        }
108
109
        return $this->type_map[$optionType->ID];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $optionType does not seem to be defined for all execution paths leading up to this point.
Loading history...
110
    }
111
}
112