Completed
Push — master ( ab9bf2...7d6b3f )
by Fabian
09:56 queued 01:25
created

CustomerFixture::logout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace TddWizard\Fixtures\Customer;
5
6
use Magento\Customer\Api\Data\AddressInterface;
7
use Magento\Customer\Api\Data\CustomerInterface;
8
use Magento\Customer\Model\Session;
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 9
    public function __construct(CustomerInterface $customer)
22
    {
23 9
        $this->customer = $customer;
24 9
    }
25
26
    public function getDefaultShippingAddressId(): int
27
    {
28
        return (int) $this->customer->getDefaultShipping();
29
    }
30
31
    public function getDefaultBillingAddressId(): int
32
    {
33
        return (int) $this->customer->getDefaultBilling();
34
    }
35
36
    public function getOtherAddressId(): int
37
    {
38
        return $this->getNonDefaultAddressIds()[0];
39
    }
40
41
    /**
42
     * @return int[]
43
     */
44
    public function getNonDefaultAddressIds(): array
45
    {
46
        return array_values(
47
            array_diff(
48
                $this->getAllAddressIds(),
49
                [$this->getDefaultBillingAddressId(), $this->getDefaultShippingAddressId()]
50
            )
51
        );
52
    }
53
54
    /**
55
     * @return int[]
56
     */
57
    public function getAllAddressIds(): array
58
    {
59
        return array_map(
60
            function (AddressInterface $address): int {
61
                return $address->getId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $address->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...
62
            },
63
            $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

63
            /** @scrutinizer ignore-type */ $this->customer->getAddresses()
Loading history...
64
        );
65
    }
66
67 8
    public function getId(): int
68
    {
69 8
        return (int) $this->customer->getId();
70
    }
71
72
    public function getConfirmation(): string
73
    {
74
        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...
75
    }
76
77
    public function getEmail(): string
78
    {
79
        return $this->customer->getEmail();
80
    }
81
82
    public function login(Session $session = null): void
83
    {
84
        if ($session === null) {
85
            $objectManager = Bootstrap::getObjectManager();
86
            $objectManager->removeSharedInstance(Session::class);
87
            $session = $objectManager->get(Session::class);
88
        }
89
        $session->setCustomerId($this->getId());
90
    }
91
92
    public function logout(Session $session = null): void
93
    {
94
        if ($session === null) {
95
            $objectManager = Bootstrap::getObjectManager();
96
            $session = $objectManager->get(Session::class);
97
        }
98
99
        $session->logout();
100
    }
101
102
    public function rollback(): void
103
    {
104
        CustomerFixtureRollback::create()->execute($this);
105
    }
106
}
107