Completed
Push — master ( b17741...33aff1 )
by Ricardo
01:11
created

PersonService::update()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 2
1
<?php
2
3
namespace Telefonica\Services;
4
5
use Informate\Models\Refund;
6
use SierraTecnologia\Crypto\Services\Crypto;
7
use Illuminate\Support\Facades\Config;
8
use Telefonica\Repositories\PersonRepository;
9
10
class PersonService
11
{
12
    public function __construct(
13
        PersonRepository $personRepository
14
    ) {
15
        $this->repo = $personRepository;
0 ignored issues
show
Bug introduced by
The property repo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
    }
17
18
    /**
19
     * Get all Persons.
20
     *
21
     * @return Collection
22
     */
23
    public function all()
24
    {
25
        return $this->repo->all();
26
    }
27
28
    /**
29
     * Get all Persons.
30
     *
31
     * @return Illuminate\Pagination\LengthAwarePaginator
32
     */
33
    public function paginated()
34
    {
35
        return $this->repo->paginated(\Illuminate\Support\Facades\Config::get('cms.pagination', 25));
36
    }
37
38
    /**
39
     * Find the Person by ID.
40
     *
41
     * @param int $id
42
     *
43
     * @return Persons
44
     */
45
    public function find($id)
46
    {
47
        return $this->repo->find($id);
48
    }
49
50
    /**
51
     * Search the orders.
52
     *
53
     * @param array $payload
54
     *
55
     * @return Collection
56
     */
57
    public function search($payload)
58
    {
59
        return $this->repo->search($payload, \Illuminate\Support\Facades\Config::get('cms.pagination', 25));
60
    }
61
62
    /**
63
     * Create an order.
64
     *
65
     * @param array $payload
66
     *
67
     * @return Persons
68
     */
69
    public function create($payload)
70
    {
71
        $order = $this->repo->store($payload);
72
73
        $this->logistics->orderCreated($order);
0 ignored issues
show
Bug introduced by
The property logistics does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
75
        return $order;
76
    }
77
78
    /**
79
     * Update an order.
80
     *
81
     * @param int   $id
82
     * @param array $payload
83
     *
84
     * @return Persons
85
     */
86
    public function update($id, $payload)
87
    {
88
        $order = $this->find($id);
89
90
        if (isset($payload['is_shipped']) && $payload['is_shipped'] !== false) {
91
            $this->logistics->shipPerson($order);
92
        }
93
94
        return $this->repo->update($order, $payload);
95
    }
96
97
    /**
98
     * Cancel an order.
99
     *
100
     * @param int $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
101
     *
102
     * @return Persons
103
     */
104
    public function cancel($orderId)
105
    {
106
        $order = $this->repo->find($orderId);
107
108
        if ($order->status != 'complete') {
109
            $this->logistics->cancelPerson($order);
110
111
            if ($order->hasActivePersonItems()) {
112
                $refund = $this->transactions->refund($order->transaction('uuid'), $order->remainingValue());
0 ignored issues
show
Bug introduced by
The property transactions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
113
114
                if ($refund) {
115
                    $refundRecord = app(Refund::class)->create(
116
                        [
117
                        'transaction_id' => $order->transaction('id'),
118
                        'provider_id' => $refund->id,
119
                        'uuid' => Crypto::uuid(),
120
                        'amount' => $refund->amount,
121
                        'provider' => 'SierraTecnologia',
122
                        'charge' => $refund->charge,
123
                        'currency' => $refund->currency,
124
                        ]
125
                    );
126
127
                    foreach ($order->items as $item) {
128
                        $item->update(
129
                            [
130
                            'was_refunded' => true,
131
                            'status' => 'cancelled',
132
                            'refund_id' => $refundRecord->id,
133
                            ]
134
                        );
135
                    }
136
137
                    return $this->update(
138
                        $order->id, [
139
                        'status' => 'cancelled',
140
                        'is_shipped' => false,
141
                        ]
142
                    );
143
                }
144
            }
145
        }
146
147
        return false;
148
    }
149
}
150