SyncEloquentRepository::update()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 34
rs 8.5806
1
<?php namespace Algorit\Synchronizer\Storage;
2
3
use Exception;
4
5
final class SyncEloquentRepository implements SyncRepositoryInterface {
6
7
	/**
8
	 * The entity instance.
9
	 *
10
	 * @var \Algorit\Synchronizer\Storage\Sync
11
	 */	
12
	protected $entity;
13
14
	/**
15
	 * The current instance.
16
	 *
17
	 * @var object
18
	 */	
19
	protected $current;
20
21
	/**
22
	 * Define the entity instance
23
	 *
24
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
25
	 */
26
	public function __construct(Sync $entity)
27
	{
28
		$this->entity = $entity;
29
	}
30
31
	/**
32
	 * Create a new model.
33
	 *
34
	 * @return Collection
35
	 */
36
	public function create($data)
37
	{
38
		return $this->entity->create($data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->entity->create($data); (Algorit\Synchronizer\Storage\Sync) is incompatible with the return type declared by the interface Algorit\Synchronizer\Sto...sitoryInterface::create of type Algorit\Synchronizer\Storage\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
39
	}
40
	
41
	/**
42
	 * Update the fillable data given an instance.
43
	 *
44
	 * @return Collection
45
	 */
46
	public function update($instance, $data)
47
	{
48
		if($instance == false)
49
		{
50
			return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('response' => false); (array<string,false>) is incompatible with the return type declared by the interface Algorit\Synchronizer\Sto...sitoryInterface::update of type Algorit\Synchronizer\Storage\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
51
	            'response' => false
52
	        );
53
		}
54
55
		$fillable = $this->entity->getFillable();
56
57
        $updated = array();
58
        $skipped = array();
59
60
        foreach($data as $key => $value)
61
        {
62
            if( ! in_array($key, $fillable))
63
            {
64
            	$skipped[$key] = $value;
65
66
                continue;
67
            }
68
           
69
            $updated[$key]  = $value;
70
71
            $instance->$key = $value;
72
        }
73
        
74
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('response' ...'skipped' => $skipped); (array) is incompatible with the return type declared by the interface Algorit\Synchronizer\Sto...sitoryInterface::update of type Algorit\Synchronizer\Storage\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
75
            'response' => $instance->save(),
76
            'updated'  => $updated,
77
            'skipped'  => $skipped
78
        );
79
	}
80
81
	/**
82
	 * Get last sync date
83
	 *
84
	 * @param  mixed   $resource
85
	 * @param  string  $entity
86
	 * @param  string  $type
87
	 * @return \Algorit\Synchronizer\Storage\Sync
88
	 */
89
	public function getLastSync($resource, $entity, $type)
90
	{
91
		$last = $this->entity->where('status', 'success')
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Algorit\Synchronizer\Storage\Sync>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
92
							 ->where('entity', $entity)
93
							 ->where('type', $type);
94
95
		return $last->orderBy('created_at', 'DESC')->first(array('created_at'));
96
	}
97
98
	/**
99
	 * Set the current sync instance
100
	 *
101
	 * @param  \Algorit\Synchronizer\Storage\Sync  $instance
102
	 * @return \Algorit\Synchronizer\Storage\Sync
103
	 */
104
	public function setCurrentSync($instance)
105
	{
106
		return $this->current = $instance;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->current = $instance; (Algorit\Synchronizer\Storage\Sync) is incompatible with the return type declared by the interface Algorit\Synchronizer\Sto...terface::setCurrentSync of type Application\Storage\Entities\Sync.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
107
	}
108
109
	/**
110
	 * Get the current sync
111
	 *
112
	 * @param  void
113
	 * @return \Algorit\Synchronizer\Storage\Sync
114
	 */
115
	public function getCurrentSync()
116
	{
117
		return $this->current;
118
	}
119
120
	/**
121
	 * Update the current sync
122
	 *
123
	 * @param  array  $data
124
	 * @return \Algorit\Synchronizer\Storage\Sync
125
	 */
126
	public function updateCurrentSync(Array $data)
127
	{
128
		return $this->update($this->current, $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->update($this->current, $data); (array<string,false>) is incompatible with the return type declared by the interface Algorit\Synchronizer\Sto...face::updateCurrentSync of type Algorit\Synchronizer\Storage\SyncEntity.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
129
	}
130
131
	/**
132
	 * Touch the current sync timestamps.
133
	 *
134
	 * @param  void
135
	 * @return \Algorit\Synchronizer\Storage\Sync
136
	 */
137
	public function touchCurrentSync()
138
	{
139
		return $this->current->touch();
140
	}
141
	
142
	/**
143
	 * Update the current sync using an exception
144
	 *
145
	 * @param  \Exception  $exception
146
	 * @return \Algorit\Synchronizer\Storage\Sync
147
	 */
148
	public function updateFailedSync(Exception $exception)
149
	{	
150
		return $this->updateCurrentSync(array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->updateCurr...ception->getLine())))); (array<string,false>) is incompatible with the return type declared by the interface Algorit\Synchronizer\Sto...rface::updateFailedSync of type Algorit\Synchronizer\Storage\SyncEntity.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
151
			'status'   => 'fail',
152
			'response' => json_encode(array(
153
				'exception'	=> get_class($exception),
154
				'message' 	=> $exception->getMessage(),
155
				'code'	  	=> $exception->getCode(),
156
				'file'	  	=> $exception->getFile(),
157
				'line'	  	=> $exception->getLine()
158
			))
159
		));
160
	}
161
}