Completed
Push — allow-no-default-tax-zone-in-c... ( 67cea0 )
by Kamil
06:23
created

OrderContext::getLatestOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Transform;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
18
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
19
use Webmozart\Assert\Assert;
20
21
final class OrderContext implements Context
22
{
23
    /** @var CustomerRepositoryInterface */
24
    private $customerRepository;
25
26
    /** @var OrderRepositoryInterface */
27
    private $orderRepository;
28
29
    public function __construct(
30
        CustomerRepositoryInterface $customerRepository,
31
        OrderRepositoryInterface $orderRepository
32
    ) {
33
        $this->customerRepository = $customerRepository;
34
        $this->orderRepository = $orderRepository;
35
    }
36
37
    /**
38
     * @Transform :order
39
     */
40
    public function getOrderByNumber($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...
41
    {
42
        $orderNumber = $this->getOrderNumber($orderNumber);
43
        $order = $this->orderRepository->findOneBy(['number' => $orderNumber]);
44
45
        Assert::notNull($order, sprintf('Cannot find order with number %s', $orderNumber));
46
47
        return $order;
48
    }
49
50
    /**
51
     * @Transform /^latest order$/
52
     */
53
    public function getLatestOrder()
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...
54
    {
55
        $orders = $this->orderRepository->findLatest(1);
56
57
        Assert::notEmpty($orders, 'No order have been made');
58
59
        return $orders[0];
60
    }
61
62
    /**
63
     * @Transform /^this order made by "([^"]+)"$/
64
     * @Transform /^order placed by "([^"]+)"$/
65
     * @Transform /^the order of "([^"]+)"$/
66
     */
67
    public function getOrderByCustomer($email)
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...
68
    {
69
        $customer = $this->customerRepository->findOneBy(['email' => $email]);
70
        Assert::notNull($customer, sprintf('Cannot find customer with email %s.', $email));
71
72
        $orders = $this->orderRepository->findByCustomer($customer);
73
        Assert::notEmpty($orders);
74
75
        return end($orders);
76
    }
77
78
    /**
79
     * @Transform :orderNumber
80
     * @Transform /^an order "([^"]+)"$/
81
     * @Transform /^the order "([^"]+)"$/
82
     */
83
    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...
84
    {
85
        return str_replace('#', '', $orderNumber);
86
    }
87
}
88