OrderDetailFactory::setOrderDetails()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
c 1
b 0
f 0
nc 10
nop 0
dl 0
loc 40
rs 8.9617
1
<?php
2
3
namespace Dynamic\Foxy\Orders\Factory;
4
5
use Dynamic\Foxy\Model\FoxyHelper;
6
use Dynamic\Foxy\Model\Variation;
7
use Dynamic\Foxy\Orders\Model\OrderDetail;
8
use Dynamic\Foxy\Orders\Model\OrderVariation;
9
use Dynamic\Foxy\Products\Page\ShippableProduct;
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Products\Page\ShippableProduct was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SilverStripe\ORM\ArrayList;
11
use SilverStripe\View\ArrayData;
12
13
/**
14
 * Class OrderDetailFactory
15
 * @package Dynamic\Foxy\Orders\Factory
16
 */
17
class OrderDetailFactory extends FoxyFactory
18
{
19
    /**
20
     * @var ArrayList
21
     */
22
    private $order_details;
23
24
    /**
25
     * @return $this
26
     * @throws \SilverStripe\ORM\ValidationException
27
     */
28
    protected function setOrderDetails()
29
    {
30
        $details = ArrayList::create();
31
32
        /** @var ArrayList $products */
33
        $products = $this->getTransaction()->getParsedTransactionData()->products;
34
35
        /** @var ArrayData $detail */
36
        foreach ($products as $detail) {
37
            $orderDetail = OrderDetail::create();
38
39
            foreach ($this->config()->get('order_detail_mapping') as $foxy => $ssFoxy) {
40
                if ($detail->hasField($foxy)) {
41
                    $orderDetail->{$ssFoxy} = $detail->getField($foxy);
42
                }
43
            }
44
45
            $codeFilter = function (\Page $page) use ($detail) {
46
                return $page->Code == $detail->getField('product_code');
47
            };
48
49
            if ($product = FoxyHelper::singleton()->getProducts()->filterByCallback($codeFilter)->first()) {
50
                $orderDetail->ProductID = $product->ID;
51
            } elseif ($variation = Variation::get()->filter('FinalCode', $detail->getField('product_code'))->first()) {
52
                $orderDetail->ProductID = $variation->ProductID;
53
            }
54
55
            $orderDetail->write();
56
57
            $orderDetail->OrderVariations()->addMany(OrderVariationFactory::create(
58
                $detail,
59
                $orderDetail->ProductID
60
            )->getOrderVariations());
61
62
            $details->push($orderDetail);
63
        }
64
65
        $this->order_details = $details;
66
67
        return $this;
68
    }
69
70
    /**
71
     * @return ArrayList
72
     * @throws \SilverStripe\ORM\ValidationException
73
     */
74
    public function getOrderDetails()
75
    {
76
        if (!$this->order_details) {
77
            $this->setOrderDetails();
78
        }
79
80
        return $this->order_details;
81
    }
82
}
83