Completed
Push — master ( 42c024...4d45bc )
by Jared
01:47
created

SoftDelete::restore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Pulsar\Traits;
4
5
use BadMethodCallException;
6
use Pulsar\Query;
7
use Pulsar\Type;
8
use Pulsar\Validator;
9
10
/**
11
 * Allows models to be soft deleted.
12
 *
13
 * @property bool     $deleted
14
 * @property int|null $deleted_at
15
 */
16
trait SoftDelete
17
{
18 View Code Duplication
    protected static function autoDefinitionSoftDelete(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20
        static::$properties['deleted'] = [
21
            'type' => Type::BOOLEAN,
22
        ];
23
        static::$properties['deleted_at'] = [
24
            'type' => Type::DATE,
25
            'validate' => 'timestamp|db_timestamp',
26
            'null' => true,
27
        ];
28
    }
29
30
    protected function performDelete(): bool
31
    {
32
        $updateArray = [
33
            'deleted' => true,
34
            'deleted_at' => time(),
35
        ];
36
        foreach ($updateArray as $k => &$v) {
37
            Validator::validateProperty($this, static::definition()->get($k), $v);
0 ignored issues
show
Bug introduced by
The method definition() does not exist on Pulsar\Traits\SoftDelete. Did you maybe mean autoDefinitionSoftDelete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
38
        }
39
40
        $updated = static::getDriver()->updateModel($this, $updateArray);
41
        if ($updated) {
42
            $this->_values['deleted'] = true;
0 ignored issues
show
Bug introduced by
The property _values 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...
43
            $this->_values['deleted_at'] = time();
44
        }
45
46
        return $updated;
47
    }
48
49
    /**
50
     * Restores a soft-deleted model.
51
     */
52
    public function restore(): bool
53
    {
54
        if (!$this->deleted) {
55
            throw new BadMethodCallException('restore() can only be called on a deleted model');
56
        }
57
58
        $this->deleted = false;
59
        $this->deleted_at = null;
60
61
        return $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
    }
63
64
    /**
65
     * Generates a new query instance that excludes soft-deleted models.
66
     */
67
    public static function withoutDeleted(): Query
68
    {
69
        return static::query()->where('deleted', false);
70
    }
71
72
    public function isDeleted(): bool
73
    {
74
        return $this->deleted;
75
    }
76
}
77