Likeable::likes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Libraries\Likeable;
4
5
use App\Libraries\Likeable\LikeCounter;
6
use Conner\Likeable\LikeableTrait;
7
8
trait Likeable 
9
{
10
	use LikeableTrait;
11
12
	public function like($type = 'like', $userId = null)
13
	{
14
		if(is_null($userId)) {
15
			$userId = $this->loggedInUserId();
16
		}
17
		
18
		if($userId) {
19
			$like = $this->likes()
20
				->where('user_id', '=', $userId)
21
				->typelike($type)
22
				->first();
23
	
24
			if($like) return;
25
	
26
			$like = new Like();
27
			$like->user_id = $userId;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<App\Libraries\Likeable\Like>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
28
			$like->type = $type;
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<App\Libraries\Likeable\Like>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
29
			$this->likes()
30
				->typelike($type)
31
				->save($like);
32
		}
33
34
		$this->incrementLikeCount($type);
35
	}
36
37
	public function unlike($type = 'like', $userId = null)
38
	{
39
		if(is_null($userId)) {
40
			$userId = $this->loggedInUserId();
41
		}
42
		
43
		if($userId) {
44
			$like = $this->likes()
45
				->where('user_id', '=', $userId)
46
				->typelike($type)
47
				->first();
48
			
49
			if(!$like) { return; }
50
	
51
			$like->delete();
52
		}
53
54
		$this->decrementLikeCount($type);
55
	}
56
57
	public function wasLiked($type = 'like', $userId = null) {
58
		if(is_null($userId)) {
59
			$userId = $this->loggedInUserId();
60
		}
61
		
62
		return (bool) $this->likes()
63
			->where('user_id', '=', $userId)
64
			->typelike($type)
65
			->count();
66
	}
67
68
	private function incrementLikeCount($type ='like')
69
	{
70
		$counter = $this->likeCounter()
71
				->typelike($type)
72
				->first();
73
		
74
		if($counter) {
75
			$counter->count++;
76
			$counter->save();
77
		} else {
78
			$counter = new LikeCounter;
79
			$counter->type = $type;
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<App\Libraries\Likeable\LikeCounter>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
80
			$counter->count = 1;
0 ignored issues
show
Documentation introduced by
The property count does not exist on object<App\Libraries\Likeable\LikeCounter>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81
			$this->likeCounter()
82
				->typelike($type)
83
				->save($counter);
84
		}
85
	}
86
	
87
	/**
88
	 * Private. Decrement the total like count stored in the counter
89
	 */
90
	private function decrementLikeCount($type ='like')
91
	{
92
		$counter = $this->likeCounter()
93
			->typelike($type)
94
			->first();
95
96
		if($counter) {
97
			$counter->count--;
98
			if($counter->count) {
99
				$counter->type = $type;
100
				$counter->save();
101
			} else {
102
				$counter->delete();
103
			}
104
		}
105
	}
106
107
	public function likes($type = 'like')
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
	{
109
		return $this->morphMany(Like::class, 'likable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
110
	}
111
112
	public function getLikes($type = 'like')
113
	{
114
		return $this->likes()->typelike($type)->get();
115
	}
116
117
	public function likeCounter()
118
	{
119
		return $this->morphOne(LikeCounter::class, 'likable');
0 ignored issues
show
Bug introduced by
It seems like morphOne() 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...
120
	}
121
}