Completed
Branch master (b23e60)
by Kamil
35:19
created

OrderContext::getOrderByCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 10
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Context\Transform;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Component\Core\Model\OrderInterface;
16
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
17
use Sylius\Component\User\Repository\CustomerRepositoryInterface;
18
use Webmozart\Assert\Assert;
19
20
/**
21
 * @author Grzegorz Sadowski <[email protected]>
22
 */
23
final class OrderContext implements Context
24
{
25
    /**
26
     * @var CustomerRepositoryInterface
27
     */
28
    private $customerRepository;
29
30
    /**
31
     * @var OrderRepositoryInterface
32
     */
33
    private $orderRepository;
34
35
    /**
36
     * @param CustomerRepositoryInterface $customerRepository
37
     * @param OrderRepositoryInterface $orderRepository
38
     */
39
    public function __construct(
40
        CustomerRepositoryInterface $customerRepository,
41
        OrderRepositoryInterface $orderRepository
42
    ) {
43
        $this->customerRepository = $customerRepository;
44
        $this->orderRepository = $orderRepository;
45
    }
46
47
    /**
48
     * @Transform :order
49
     */
50 View Code Duplication
    public function getOrderByNumber($orderNumber)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $orderNumber = $this->getOrderNumber($orderNumber);
53
        $order = $this->orderRepository->findOneBy(['number' => $orderNumber]);
54
55
        Assert::notNull($order, sprintf('Cannot find order with number %s', $orderNumber));
56
57
        return $order;
58
    }
59
60
    /**
61
     * @Transform /^this order made by "([^"]+)"$/
62
     */
63 View Code Duplication
    public function getOrderByCustomer($email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $customer = $this->customerRepository->findOneBy(['email' => $email]);
66
        Assert::notNull($customer, sprintf('Cannot find customer with email %s.', $email));
67
68
        $orders = $this->orderRepository->findByCustomer($customer);
69
        Assert::notEmpty($orders);
70
71
        return end($orders);
72
    }
73
74
    /**
75
     * @Transform :orderNumber
76
     * @Transform /^an order "([^"]+)"$/
77
     */
78
    public function getOrderNumber($orderNumber)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
79
    {
80
        $orderNumber = str_replace('#', '', $orderNumber);
81
82
        return $orderNumber;
83
    }
84
}
85