HasImages   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 106
c 1
b 0
f 0
rs 10
wmc 10
lcom 2
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
getFromRelationshipByName() 0 1 ?
A images() 0 4 1
A hasImages() 0 4 1
A image() 0 4 1
A imageUrl() 0 8 2
A imageCaption() 0 8 2
A imageAlt() 0 8 2
A imagesWithProps() 0 7 1
1
<?php
2
3
namespace Larafolio\Models\Traits;
4
5
use Larafolio\Models\Image;
6
7
trait HasImages
8
{
9
    /**
10
     * Get a model from a relationship by model name.
11
     *
12
     * @param string $relationship Name of relationship.
13
     * @param string $name         Name of model to get.
14
     *
15
     * @return \Illuminate\Database\Eloquent\Model|null
16
     */
17
    abstract protected function getFromRelationshipByName($relationship, $name);
18
19
    /**
20
     * A project has many images.
21
     *
22
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
23
     */
24
    public function images()
25
    {
26
        return $this->hasMany(Image::class);
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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...
27
    }
28
29
    /**
30
     * Return true if project has images.
31
     *
32
     * @return bool
33
     */
34
    public function hasImages()
35
    {
36
        return !$this->images->isEmpty();
0 ignored issues
show
Bug introduced by
The property images 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...
37
    }
38
39
    /**
40
     * Get image by name, if exists.
41
     *
42
     * @param string $name Name of image to get.
43
     *
44
     * @return Larafolio\Models\Image|null
45
     */
46
    public function image($name)
47
    {
48
        return $this->getFromRelationshipByName('images', $name);
49
    }
50
51
    /**
52
     * Get image url for given size.
53
     *
54
     * @param string $name Name of image to get url for.
55
     * @param string $size Size of image.
56
     *
57
     * @return string|null
58
     */
59
    public function imageUrl($name, $size = 'medium')
60
    {
61
        if (!$image = $this->image($name)) {
62
            return;
63
        }
64
65
        return $image->{$size}();
66
    }
67
68
    /**
69
     * Get caption for image.
70
     *
71
     * @param string $name Name of image to get caption for.
72
     *
73
     * @return string|null
74
     */
75
    public function imageCaption($name)
76
    {
77
        if (!$image = $this->image($name)) {
78
            return;
79
        }
80
81
        return $image->caption();
82
    }
83
84
    /**
85
     * Get alt for image.
86
     *
87
     * @param string $name Name of image to get caption for.
88
     *
89
     * @return string|null
90
     */
91
    public function imageAlt($name)
92
    {
93
        if (!$image = $this->image($name)) {
94
            return;
95
        }
96
97
        return $image->alt();
98
    }
99
100
    /**
101
     * Return images with all props needed for javascript.
102
     *
103
     * @return Collection
104
     */
105
    public function imagesWithProps()
106
    {
107
        return $this->images
108
            ->map(function (Image $image) {
109
                return $image->generateProps();
110
            })->reverse()->values();
111
    }
112
}
113