Test Failed
Push — master ( 48ddb2...dea459 )
by David
11:07
created

CartFlow::loadStepsConfig()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 1
nop 0
dl 0
loc 19
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GGGGino\SkuskuCartBundle\Form;
4
5
use Craue\FormFlowBundle\Form\FormFlow;
6
use Craue\FormFlowBundle\Form\FormFlowInterface;
7
use GGGGino\SkuskuCartBundle\Entity\CartForm;
8
use GGGGino\SkuskuCartBundle\Event\PostPaymentCartEvent;
9
use GGGGino\SkuskuCartBundle\Event\PostSubmitCartEvent;
10
use GGGGino\SkuskuCartBundle\Event\PreSubmitCartEvent;
11
use GGGGino\SkuskuCartBundle\Model\SkuskuCart;
12
use GGGGino\SkuskuCartBundle\Model\SkuskuPayment;
13
use GGGGino\SkuskuCartBundle\Service\CartManager;
14
use GGGGino\SkuskuCartBundle\Service\OrderManager;
15
use Payum\Core\Gateway;
16
use Payum\Core\GatewayInterface;
17
use Payum\Core\Payum;
18
use Payum\Core\Request\Capture;
19
use Symfony\Component\Form\FormInterface;
20
use Symfony\Component\HttpFoundation\JsonResponse;
21
use Symfony\Component\HttpFoundation\RedirectResponse;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\RequestStack;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\HttpFoundation\Session\Session;
26
27
class CartFlow extends CartFlowBase
28
{
29
    const PRE_SUBMIT = 'skusku_cart.pre_submit';
30
31
    const POST_SUBMIT = 'skusku_cart.post_submit';
32
33
    const POST_PAYMENT = 'skusku_cart.post_payment';
34
35
    /**
36
     * @var CartManager
37
     */
38
    private $cartManager;
39
40
    /**
41
     * @var RequestStack
42
     */
43
    private $requestStack;
44
45
    /**
46
     * @var Payum
47
     */
48
    private $payum;
49
50
    /**
51
     * @var OrderManager
52
     */
53
    private $orderManager;
54
55
    /**
56
     * CartFlowBase constructor.
57
     * @param array $configSteps
58
     * @param CartManager $cartManager
59
     * @param OrderManager $orderManager
60
     * @param RequestStack $requestStack
61
     */
62
    public function __construct(
63
        array $configSteps,
64
        CartManager $cartManager,
65
        OrderManager $orderManager,
66
        RequestStack $requestStack)
67
    {
68
        parent::__construct($configSteps);
69
70
        $this->cartManager = $cartManager;
71
        $this->requestStack = $requestStack;
72
        $this->orderManager = $orderManager;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    protected function loadStepsConfig()
79
    {
80
        $this->configSteps['payment']['skip'] = function($estimatedCurrentStepNumber, FormFlowInterface $flow) {
81
            /** @var CartForm $data */
82
            $data = $flow->getFormData();
83
84
            $paymentMethod = $data->getPaymentMethod();
85
86
            // se non è settato vuol dire che non ci sono ancora arrivato
87
            if( !$paymentMethod )
0 ignored issues
show
introduced by
$paymentMethod is of type string, thus it always evaluated to true.
Loading history...
88
                return false;
89
90
            /** @var GatewayInterface $gateway */
91
            $gateway = $this->payum->getGateway($paymentMethod);
0 ignored issues
show
Unused Code introduced by
The assignment to $gateway is dead and can be removed.
Loading history...
92
93
            return true;
94
        };
95
96
        return $this->configSteps;
97
    }
98
99
    /**
100
     * @param $form
101
     * @param CartForm $formData
102
     * @return Response|void
103
     *
104
     * @throws \Payum\Core\Reply\ReplyInterface
105
     * @throws null
106
     */
107
    public function handleSubmit(&$form, $formData)
108
    {
109
        if ($this->isValid($form)) {
110
            $this->saveCurrentStepData($form);
111
112
            if ($this->nextStep()) {
113
                // form for the next step
114
                $form = $this->createForm();
115
            } else {
116
                // flow finished
117
                /** @var SkuskuCart $finalCart */
118
                $finalCart = $formData->getCart();
119
120
                if ($this->hasListeners(self::PRE_SUBMIT)) {
121
                    $event = new PreSubmitCartEvent($this, $formData);
0 ignored issues
show
Bug introduced by
$formData of type GGGGino\SkuskuCartBundle\Entity\CartForm is incompatible with the type GGGGino\SkuskuCartBundle\Model\SkuskuCart expected by parameter $cart of GGGGino\SkuskuCartBundle...artEvent::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
                    $event = new PreSubmitCartEvent($this, /** @scrutinizer ignore-type */ $formData);
Loading history...
122
                    $this->eventDispatcher->dispatch(self::PRE_SUBMIT, $event);
123
                }
124
125
                $this->cartManager->persistCart($finalCart);
126
127
                $storage = $this->payum->getStorage('GGGGino\SkuskuCartBundle\Model\SkuskuPayment');
128
129
                $payment = new SkuskuPayment();
130
                $payment->setNumber(uniqid());
131
                $payment->setCurrencyCode($finalCart->getCurrency()->getIsoCode());
132
                $payment->setTotalAmount($finalCart->getTotalPrice()); // 1.23 EUR
133
                $payment->setDescription($finalCart->getTotalQuantity());
134
                $payment->setClientId($finalCart->getCustomer());
0 ignored issues
show
Bug introduced by
$finalCart->getCustomer() of type GGGGino\SkuskuCartBundle...SkuskuCustomerInterface is incompatible with the type string expected by parameter $clientId of Payum\Core\Model\Payment::setClientId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
                $payment->setClientId(/** @scrutinizer ignore-type */ $finalCart->getCustomer());
Loading history...
135
                $payment->setClientEmail($finalCart->getCustomer()->getEmail());
136
137
                $storage->update($payment);
138
139
                $captureToken = $this->payum->getTokenFactory()->createCaptureToken(
140
                    $formData->getPaymentMethod(),
141
                    $payment,
142
                    'done' // the route to redirect after capture
143
                );
144
145
                $finalCart->setPayment($payment);
146
147
                $this->cartManager->flushCart($finalCart);
148
149
                $this->reset(); // remove step data from the session
150
151
                if ($this->hasListeners(self::POST_SUBMIT)) {
152
                    $event = new PostSubmitCartEvent($this, $formData);
0 ignored issues
show
Bug introduced by
$formData of type GGGGino\SkuskuCartBundle\Entity\CartForm is incompatible with the type GGGGino\SkuskuCartBundle\Model\SkuskuCart expected by parameter $cart of GGGGino\SkuskuCartBundle...artEvent::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
                    $event = new PostSubmitCartEvent($this, /** @scrutinizer ignore-type */ $formData);
Loading history...
153
                    $this->eventDispatcher->dispatch(self::POST_SUBMIT, $event);
154
                }
155
156
                return new RedirectResponse($captureToken->getTargetUrl());
157
            }
158
        }
159
    }
160
161
    /**
162
     * Excecute all the listener tagged with "skusku_cart.post_payment"
163
     * Save the cart as order
164
     *
165
     * @param $payment
166
     * @return FormInterface
167
     */
168
    public function handleDone(SkuskuPayment $payment, $status)
169
    {
170
        if ($this->hasListeners(self::POST_PAYMENT)) {
171
            $event = new PostPaymentCartEvent($this, $payment, $status);
172
            $this->eventDispatcher->dispatch(self::POST_PAYMENT, $event);
173
        }
174
175
        $order = $this->orderManager->buildOrderFromCart($payment->getCart());
176
        $this->orderManager->saveOrder($order);
177
178
        // you have order and payment status
179
        // so you can do whatever you want for example you can just print status and payment details.
180
    }
181
182
    /**
183
     * @param mixed $payum
184
     * @return CartFlow
185
     */
186
    public function setPayum($payum)
187
    {
188
        $this->payum = $payum;
189
        return $this;
190
    }
191
}