Passed
Push — master ( 00e9c5...880156 )
by Fabian
78:38 queued 33:46
created

AddToCartGraphQl::getProduct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\AddToCartGraphQl\ViewModel;
5
6
use Magento\Catalog\Api\Data\ProductInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Catalog\Api\Data\ProductInterface 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...
7
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
0 ignored issues
show
Bug introduced by
The type Magento\ConfigurableProd...oduct\Type\Configurable 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...
8
use Magento\Framework\View\Element\Block\ArgumentInterface;
9
use Hyva\Theme\ViewModel\CurrentProduct;
0 ignored issues
show
Bug introduced by
The type Hyva\Theme\ViewModel\CurrentProduct 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
11
class AddToCartGraphQl implements ArgumentInterface
12
{
13
    private CurrentProduct $currentProduct;
0 ignored issues
show
Coding Style introduced by
PHP syntax error: syntax error, unexpected 'CurrentProduct' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
Loading history...
14
15
    public function __construct(
16
        CurrentProduct $currentProduct)
0 ignored issues
show
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
17
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
18
        $this->currentProduct = $currentProduct;
19
    }
20
21
    private function getProduct() : ProductInterface
22
    {
23
        return $this->currentProduct->get();
24
    }
25
26
    public function isProductConfigurable(): bool
27
    {
28
        return $this->getProduct()->getTypeId() === Configurable::TYPE_CODE;
29
    }
30
31
    public function getAddToCartQuery(): string
32
    {
33
        if ($this->isProductConfigurable()) {
34
            return $this->getAddToCartQueryProductTypeConfigurable();
35
        }
36
37
        return $this->getAddToCartQueryProductTypeSimple();
38
    }
39
40
    private function getAddToCartQueryProductTypeSimple(): string
41
    {
42
        return '{
43
        addProductsToCart(
44
            cartId: "%cartId"
45
            cartItems: [
46
              {
47
                quantity: %qty
48
                sku: "%sku"
49
              }
50
            ]
51
        ) {
52
        cart {
53
          items {
54
            product {
55
              name
56
              sku
57
            }
58
            quantity
59
          }
60
        }
61
      }
62
    }';
63
    }
64
65
    private function getAddToCartQueryProductTypeConfigurable(): string
66
    {
67
        return '{
68
            addProductsToCart(
69
                cartId: "%cartId"
70
                cartItems: [
71
                  {
72
                    quantity: %qty
73
                    sku: "%sku"
74
                    selected_options: [%selectedOptions]
75
                  }
76
                ]
77
            ) {
78
            cart {
79
              items {
80
                product {
81
                  name
82
                  sku
83
                }
84
                quantity
85
              }
86
            }
87
          }
88
        }';
89
    }
90
}
91