Completed
Push — master ( 429264...faaec3 )
by Kamil
25:12 queued 25:01
created

OrderContext::getOrderByNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
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
    public function getOrderByNumber($orderNumber)
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
    public function getOrderByCustomer($email)
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)
79
    {
80
        $orderNumber = str_replace('#', '', $orderNumber);
81
82
        return $orderNumber;
83
    }
84
}
85