Completed
Push — master ( 511710...7a7db5 )
by Fabian
22s queued 12s
created

CustomerFixture::getDefaultBillingAddressId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace TddWizard\Fixtures\Customer;
4
5
use Magento\Customer\Api\Data\AddressInterface;
6
use Magento\Customer\Api\Data\CustomerInterface;
7
use Magento\Customer\Model\Session;
8
use Magento\Framework\ObjectManagerInterface;
9
use Magento\TestFramework\Helper\Bootstrap;
0 ignored issues
show
Bug introduced by
The type Magento\TestFramework\Helper\Bootstrap 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
/**
12
 * Object that can be returned from customer fixture, contains ids for test expectations
13
 */
14
class CustomerFixture
15
{
16
    /**
17
     * @var CustomerInterface
18
     */
19
    private $customer;
20
21
    public function __construct(CustomerInterface $customer)
22
    {
23
        $this->customer = $customer;
24
    }
25
26
    public function getDefaultShippingAddressId() : int
27
    {
28
        return $this->customer->getDefaultShipping();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->customer->getDefaultShipping() returns the type null|string which is incompatible with the type-hinted return integer.
Loading history...
29
    }
30
31
    public function getDefaultBillingAddressId() : int
32
    {
33
        return $this->customer->getDefaultBilling();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->customer->getDefaultBilling() returns the type null|string which is incompatible with the type-hinted return integer.
Loading history...
34
    }
35
36
    public function getOtherAddressId() : int
37
    {
38
        return $this->getNonDefaultAddressIds()[0];
39
    }
40
41
    public function getNonDefaultAddressIds() : array
42
    {
43
        return array_values(
44
            array_diff(
45
                $this->getAllAddressIds(),
46
                [$this->getDefaultBillingAddressId(), $this->getDefaultShippingAddressId()]
47
            )
48
        );
49
    }
50
51
    public function getAllAddressIds(): array
52
    {
53
        return array_map(
54
            function (AddressInterface $address) {
55
                return $address->getId();
56
            },
57
            $this->customer->getAddresses()
0 ignored issues
show
Bug introduced by
It seems like $this->customer->getAddresses() can also be of type null; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

57
            /** @scrutinizer ignore-type */ $this->customer->getAddresses()
Loading history...
58
        );
59
    }
60
61
    public function getId() : int
62
    {
63
        return $this->customer->getId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->customer->getId() could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
64
    }
65
66
    public function getConfirmation() : string
67
    {
68
        return $this->customer->getConfirmation();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->customer->getConfirmation() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
69
    }
70
71
    public function getEmail() : string
72
    {
73
        return $this->customer->getEmail();
74
    }
75
76
    public function login(Session $session = null)
77
    {
78
        if ($session === null) {
79
            $objectManager = Bootstrap::getObjectManager();
80
            $objectManager->removeSharedInstance(Session::class);
81
            $session = $objectManager->get(Session::class);
82
        }
83
        $session->setCustomerId($this->getId());
84
    }
85
86
    public function logout(Session $session = null)
87
    {
88
        if ($session === null) {
89
            $objectManager = Bootstrap::getObjectManager();
90
            $session = $objectManager->get(Session::class);
91
        }
92
93
        $session->logout();
94
    }
95
}
96