Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
CustomerResource
100.00% covered (success)
100.00%
14 / 14
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%
10 / 10
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\V2;
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     */
71    //phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass, Squiz.Commenting.FunctionComment.Missing
72    public function toArray($request = null)
73    {
74        $data = array_intersect_key(
75            array_merge(
76                [
77                    'email' => $this->customer->email
78                ],
79                $this->login->toArray(),
80                $this->info->toArray(),
81                ['status' => $this->login->status]
82            ),
83            $this->getSkeleton()
84        );
85
86        $data['country']  = $this->info->country->value;
87        $data['address2'] = $this->info->address2 ?? '';
88        return $data;
89    }
90
91    /**
92     * Returns the resource skeletton
93     *
94     * @return array
95     */
96    public static function getSkeleton(): array
97    {
98        return self::$skeleton;
99    }
100}