Completed
Push — string-event-subscriptions ( e8cd90...1f8f4a )
by Peter
21:14
created

TrashableTrait::trash()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 40
rs 6.7272
cc 7
eloc 22
nc 10
nop 1
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
	public function trash($runValidation = true)
51
	{
52
		ScenarioManager::setScenario($this, TrashInterface::ScenarioTrash);
53
		$validator = new Validator($this);
54
		if (!$runValidation || $validator->validate())
55
		{
56
			$eventBefore = new TrashEvent($this);
57
			if (Event::hasHandler($this, TrashInterface::EventBeforeTrash))
58
			{
59
				if (!Event::valid($this, TrashInterface::EventBeforeTrash, $eventBefore))
60
				{
61
					return false;
62
				}
63
			}
64
			$meta = ManganMeta::create($this);
65
66
			$trash = $eventBefore->getTrash();
67
			if (empty($trash))
68
			{
69
				$trash = new Trash();
70
			}
71
			$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
			$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
			$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
			$trash->save();
75
76
			$eventAfter = new TrashEvent($this);
77
78
			Event::trigger($this, TrashInterface::EventAfterTrash, $eventAfter);
79
80
			// Use deleteOne, to avoid beforeDelete event,
81
			// which should be raised only when really removing document:
82
			// when emtying trash
83
84
			$em = new EntityManager($this);
85
			$em->deleteOne(PkManager::prepareFromModel($this));
86
			return true;
87
		}
88
		return false;
89
	}
90
91
	/**
92
	 * Restore trashed item
93
	 * @return boolean
94
	 * @throws Exception
95
	 * @Ignored
96
	 */
97
	public function restore()
98
	{
99
		if (!$this instanceof TrashInterface)
100
		{
101
			// When trying to restore normal document instead of trash item
102
			throw new Exception('Restore can be performed only on `%s` instance', TrashInterface::class);
103
		}
104
		$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...
105
106
		Event::trigger($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...
107
108
		$saved = $em->save();
109
		if (!$saved)
110
		{
111
			return false;
112
		}
113
		$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...
114
		$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...
115
		if (!$model)
116
		{
117
			return false;
118
		}
119
		$eventAfter = new RestoreEvent();
120
		$eventAfter->setTrashed($this);
0 ignored issues
show
Documentation introduced by
$this is of type object<Maslosoft\Mangan\...erfaces\TrashInterface>, but the function expects a object<Maslosoft\Mangan\Events\type>.

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...
121
		Event::trigger($model, TrashInterface::EventAfterRestore, $eventAfter);
122
123
		$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...
124
125
		// Use deleteOne, to avoid beforeDelete event,
126
		// which should be raised only when really removing document:
127
		// when emtying trash
128
		$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...
129
130
		$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...
131
		return true;
132
	}
133
134
}
135