Completed
Push — master ( 97e950...f11284 )
by Vitaly
02:31
created

Gallery::getCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 8
Ratio 50 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 4
Bugs 2 Features 2
Metric Value
c 4
b 2
f 2
dl 8
loc 16
rs 9.4285
ccs 10
cts 10
cp 1
cc 2
eloc 9
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: myslyvyi
5
 * Date: 05.01.2016
6
 * Time: 15:25
7
 */
8
namespace samsoncms\api;
9
10
use samson\cms\CMSGallery;
11
use samsonframework\orm\QueryInterface;
12
13
/***
14
 *  Gallery additional field for material.
15
 *  This class enables getting all information about additional fields gallery for specific material.
16
 *  @author [email protected]
17
 */
18
class Gallery
19
{
20
    /** @var integer materialFieldId Table materialField identifier */
21
    protected $materialFieldId = null;
22
23
    /** @var QueryInterface Database query interface */
24
    protected $query;
25
26
    /**
27
     * Constructor Gallery.
28
     * This constructor finds identifier additional field gallery from database record its material and field identifiers.
29
     *
30
     * @param QueryInterface $query Database query interface
31
     * @param integer $materialId material identifier
32
     * @param integer $fieldId field identifier
33
     */
34 3
    public function __construct(QueryInterface $query, $materialId, $fieldId)
35
    {
36
        /** @var object $materialField additional field value database record*/
37 3
        $materialField = null;
0 ignored issues
show
Unused Code introduced by
$materialField is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
38
39
        //set query interface
40 3
        $this->query = $query;
41
42
        //Find additional field value database record by its material and field identifiers.
43 3
        $materialField = $this->query->entity(MaterialField::ENTITY)
44 3
            ->where(Material::F_PRIMARY, $materialId)
45 3
            ->where(Field::F_PRIMARY, $fieldId)
46 3
            ->where(Material::F_DELETION, 1)
47 3
            ->first();
48
49 3
        if ($materialField) {
50
            //Set materialFieldId
51 3
            $this->materialFieldId = $materialField->id;
52 3
        }
53
54 3
    }
55
56
    /**
57
     * Getting quantity images in additional field gallery
58
     *
59
     * @return integer $count
60
     */
61 1
    public function getCount()
62
    {
63
        /**@var integer $count quantity images in additional field gallery */
64 1
        $count = 0;
65
66 1 View Code Duplication
        if ($this->hasImages()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            // Getting quantity images for gallery
68 1
            $count = $this->query
69 1
                ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
70 1
                ->where(Field::F_DELETION, 1)
71 1
                ->where(MaterialField::F_PRIMARY, $this->materialFieldId)
72 1
                ->count();
73 1
        }
74
75 1
        return $count;
76
    }
77
78
    /**
79
     * Check on empty gallery. If materialFieldId = null and quantity images not more 1 then material not has images.
80
     *
81
     * @return boolean
82
     **/
83 3
    public function hasImages()
84
    {
85
        /**@var $hasImages */
86 3
        $hasImages = false;
87
88 3 View Code Duplication
        if (isset($this->materialFieldId)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
            // Getting quantity images, if quantity more 0 then material has images
90 3
            if ($this->query
91 3
            ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
92 3
            ->where(Field::F_DELETION, 1)
93 3
            ->where(MaterialField::F_PRIMARY, $this->materialFieldId)
94 3
            ->count() > 0) {
95 3
                $hasImages = true;
96 3
            }
97 3
        }
98
99 3
        return $hasImages;
100
    }
101
102
    /**
103
     * Get collection of images for material by gallery additional field selector. If none is passed
104
     * all images from gallery table would be returned empty array.
105
     *
106
     * @param integer $currentPage current page with images. Min value = 1
107
     * @param integer $countView quantity view by page
108
     *
109
*@return array
110
     * @deprecated  Use find()
111
     */
112
    public function getImages($currentPage = null, $countView = 20)
113
    {
114
        return $this->find($currentPage, $countView);
115
    }
116
117
    /**
118
     * Perform SamsonCMS query and get entity gallery images.
119
     *
120
     * @param int $page Page number
121
     * @param int $size Page size
122
     *
123
     * @return GalleryField[] Collection of entity gallery images
124
     */
125 2
    public function find($page = null, $size = null)
126
    {
127
        /** @var GalleryField[] $images Get material images for this gallery */
128 2
        $images = array();
129
130
        /** @var QueryInterface $query Database query interface*/
131 2
        $query = null;
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
132
133 2
        if ($this->hasImages()) {
134
            // Select all images in DB by materialFieldId
135 2
            $query = $this->query
136 2
                ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
137 2
                ->where(Field::F_DELETION, 1)
138 2
                ->where(MaterialField::F_PRIMARY, $this->materialFieldId);
139
140
            // Add paging
141 2
            if (isset($page) && $page > 0) {
142
                //Set limit for query
143 1
                $query->limit(--$page * $size, $size);
144 1
            }
145
146
            // Execute query
147 2
            $images = $query->exec();
148 2
        }
149
150 2
        return $images;
151
    }
152
}
153