Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
CustomerResource
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 getSkeleton
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Resources\Api\V4;
4
5use App\Models\Brand;
6use App\Models\Customer;
7use Illuminate\Http\Resources\Json\JsonResource;
8
9class CustomerResource extends JsonResource
10{
11    /** @var \App\Models\Customer */
12    private $customer;
13
14    /** @var \App\Models\CustomerInfo */
15    private $info;
16
17    /** @var \App\Models\CustomerLogin */
18    private $login;
19
20    /** @var array */
21    private static $skeleton = [
22        'hashcode'   => '',
23        'firstname'  => '',
24        'lastname'   => '',
25        'email'      => '',
26        'birth_date' => '',
27        'address'    => '',
28        'address2'   => '',
29        'phone'      => '',
30        'country'    => '',
31        'city'       => '',
32        'postcode'   => '',
33        'opt_in'     => '',
34        'status'     => '',
35        'last_connection' => ''
36    ];
37
38    /**
39     * CustomerResource.
40     *
41     * @param \App\Models\Brand    $brand
42     * @param \App\Models\Customer $customer
43     */
44    public function __construct(Brand $brand, Customer $customer)
45    {
46        $this->customer = $customer;
47        $this->info     = $customer->info($brand, Customer::FIRST_OR_FAIL);
48        $this->login    = $customer->login($brand, Customer::FIRST_OR_FAIL);
49    }
50
51    /**
52     * Transform the resource into an array.
53     *
54     * @param \Illuminate\Http\Request|null $request
55     * @return array
56     * + email string
57     * + firstname string
58     * + lastname string
59     * + birth_date datetime
60     * + address string
61     * + address2 string
62     * + phone string
63     * + country string
64     * + city string
65     * + postcode string
66     * + opt_in boolean
67     * + hashcode string
68     * + status boolean
69     * + last_connection date
70     * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass, Squiz.Commenting.FunctionComment.Missing
71     */
72    public function toArray($request = null): array
73    {
74        //phpcs:enable
75        $data = array_intersect_key(
76            array_merge(
77                [
78                    'email' => $this->customer->email
79                ],
80                $this->login->toArray(),
81                $this->info->toArray(),
82                ['status' => $this->login->status]
83            ),
84            $this->getSkeleton()
85        );
86
87        $data['country']  = $this->info->country->value();
88        $data['address2'] = $this->info->address2 ?? '';
89        return $data;
90    }
91
92    /**
93     * Returns the resource skeletton
94     *
95     * @return array
96     */
97    public static function getSkeleton(): array
98    {
99        return self::$skeleton;
100    }
101}