Completed
Push — master ( 971f99...10707b )
by Peter
07:20 queued 02:39
created

TrashableTrait::restore()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 45
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.2163

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 45
ccs 18
cts 22
cp 0.8182
rs 8.439
c 4
b 0
f 0
cc 6
eloc 22
nc 6
nop 0
crap 6.2163
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link http://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Traits\Model;
15
16
use Exception;
17
use Maslosoft\Mangan\EntityManager;
18
use Maslosoft\Mangan\Events\Event;
19
use Maslosoft\Mangan\Events\ModelEvent;
20
use Maslosoft\Mangan\Events\RestoreEvent;
21
use Maslosoft\Mangan\Events\TrashEvent;
22
use Maslosoft\Mangan\Finder;
23
use Maslosoft\Mangan\Helpers\PkManager;
24
use Maslosoft\Mangan\Interfaces\TrashInterface;
25
use Maslosoft\Mangan\Meta\ManganMeta;
26
use Maslosoft\Mangan\Model\Trash;
27
use Maslosoft\Mangan\ScenarioManager;
28
use Maslosoft\Mangan\Validator;
29
30
/**
31
 * Uswe this trait to make model trashable
32
 *
33
 * @author Piotr
34
 */
35
trait TrashableTrait
36
{
37
38
	/**
39
	 * Move to trash. Validation will be performed
40
	 * before trashing with `trash` (TrashInterface::ScenarioTrash) scenario.
41
	 *
42
	 *
43
	 * @see TrashInterface::ScenarioTrash
44
	 * @param boolean $runValidation whether to perform validation before saving the record.
45
	 * If the validation fails, the record will not be saved to database.
46
	 *
47
	 * @return boolean
48
	 * @Ignored
49
	 */
50 4
	public function trash($runValidation = true)
51
	{
52 4
		ScenarioManager::setScenario($this, TrashInterface::ScenarioTrash);
53 4
		$validator = new Validator($this);
54 4
		if (!$runValidation || $validator->validate())
55
		{
56 4
			$eventBefore = new TrashEvent($this);
57 4
			if (Event::hasHandler($this, TrashInterface::EventBeforeTrash))
58
			{
59
				if (!Event::valid($this, TrashInterface::EventBeforeTrash, $eventBefore))
60
				{
61
					return false;
62
				}
63
			}
64 4
			$meta = ManganMeta::create($this);
65
66 4
			$trash = $eventBefore->getTrash();
67 4
			if (empty($trash))
68
			{
69 4
				$trash = new Trash();
70
			}
71 4
			$trash->name = (string) $this;
1 ignored issue
show
Bug introduced by
Accessing name on the interface Maslosoft\Mangan\Interfaces\TrashInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
72 4
			$trash->data = $this;
1 ignored issue
show
Bug introduced by
Accessing data on the interface Maslosoft\Mangan\Interfaces\TrashInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
73 4
			$trash->type = isset($meta->type()->label) ? $meta->type()->label : get_class($this);
1 ignored issue
show
Bug introduced by
Accessing type on the interface Maslosoft\Mangan\Interfaces\TrashInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
74 4
			if (!$trash->save())
75
			{
76
				return false;
77
			}
78
79 4
			$eventAfter = new TrashEvent($this);
80 4
			$eventAfter->setTrash($trash);
81
82 4
			if (!Event::valid($this, TrashInterface::EventAfterTrash, $eventAfter))
83
			{
84
				return false;
85
			}
86
87
			// Use deleteOne, to avoid beforeDelete event,
88
			// which should be raised only when really removing document:
89
			// when emtying trash
90
91 4
			$em = new EntityManager($this);
92 4
			return $em->deleteOne(PkManager::prepareFromModel($this));
93
		}
94
		return false;
95
	}
96
97
	/**
98
	 * Restore trashed item
99
	 * @return boolean
100
	 * @throws Exception
101
	 * @Ignored
102
	 */
103 3
	public function restore()
104
	{
105 3
		if (!$this instanceof TrashInterface)
106
		{
107
			// When trying to restore normal document instead of trash item
108
			throw new Exception(sprintf('Restore can be performed only on `%s` instance', TrashInterface::class));
109
		}
110 3
		$em = new EntityManager($this->data);
1 ignored issue
show
Bug introduced by
Accessing data on the interface Maslosoft\Mangan\Interfaces\TrashInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
111
112
		// Set scenario to `restore` for model, which is just about to be restored
113 3
		ScenarioManager::setScenario($this->data, TrashInterface::ScenarioRestore);
1 ignored issue
show
Bug introduced by
Accessing data on the interface Maslosoft\Mangan\Interfaces\TrashInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
114
115 3
		if (!Event::valid($this->data, TrashInterface::EventBeforeRestore))
1 ignored issue
show
Bug introduced by
Accessing data on the interface Maslosoft\Mangan\Interfaces\TrashInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
116
		{
117 1
			return false;
118
		}
119
120 2
		$saved = $em->save();
121 2
		if (!$saved)
122
		{
123
			return false;
124
		}
125 2
		$finder = new Finder($this->data);
1 ignored issue
show
Bug introduced by
Accessing data on the interface Maslosoft\Mangan\Interfaces\TrashInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
126 2
		$model = $finder->find(PkManager::prepareFromModel($this->data));
1 ignored issue
show
Bug introduced by
Accessing data on the interface Maslosoft\Mangan\Interfaces\TrashInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
127 2
		if (!$model)
128
		{
129
			return false;
130
		}
131 2
		$eventAfter = new RestoreEvent();
132 2
		$eventAfter->setTrashed($this->data);
1 ignored issue
show
Bug introduced by
Accessing data on the interface Maslosoft\Mangan\Interfaces\TrashInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
133 2
		$eventAfter->setTrash($this);
134 2
		if (!Event::valid($model, TrashInterface::EventAfterRestore, $eventAfter))
135
		{
136
			return false;
137
		}
138
139 2
		$trashEm = new EntityManager($this);
0 ignored issues
show
Documentation introduced by
$this is of type object<Maslosoft\Mangan\...erfaces\TrashInterface>, but the function expects a object<Maslosoft\Addendu...ces\AnnotatedInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
140
141 2
		$this->data = null;
1 ignored issue
show
Bug introduced by
Accessing data on the interface Maslosoft\Mangan\Interfaces\TrashInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
142
143
		// Use deleteOne, to avoid beforeDelete event,
144
		// which should be raised only when really removing document:
145
		// when emtying trash
146 2
		return $trashEm->deleteOne(PkManager::prepareFromModel($this));
0 ignored issues
show
Documentation introduced by
$this is of type object<Maslosoft\Mangan\...erfaces\TrashInterface>, but the function expects a object<Maslosoft\Addendu...ces\AnnotatedInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
	}
148
149
}
150