Completed
Pull Request — master (#6)
by Dima
11:02 queued 04:24
created

Gallery::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 7

Duplication

Lines 19
Ratio 100 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 19
loc 19
rs 9.2
cc 4
eloc 7
nc 3
nop 3
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 samsoncms\api\MaterialField;
12
use samsonframework\orm\QueryInterface;
13
14
/***
15
 *  Gallery additional field for material.
16
 *  This class enables getting all information about additional fields gallery for specific material.
17
 *  @author [email protected]
18
 */
19 View Code Duplication
class Gallery
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
20
{
21
    /** @var integer materialFieldId Table materialField identifier */
22
    protected $materialFieldId = null;
23
24
    /** @var QueryInterface Database query interface */
25
    protected $query;
26
27
    /**
28
     * Constructor Gallery.
29
     * This constructor finds identifier additional field gallery from database record its material and field identifiers.
30
     *
31
     * @param QueryInterface $query Database query interface
32
     * @param integer $materialId material identifier
33
     * @param integer $fieldId field identifier
34
     */
35
    public function __construct(QueryInterface $query, $materialId, $fieldId)
36
    {
37
        /** @var object $materialField additional field value database record*/
38
        $materialField = null;
39
40
        //set query interface
41
        $this->query = $query;
42
43
        // Checking params by type
44
        if (is_int($materialId) && is_int($fieldId)) {
45
            //Find additional field value database record by its material and field identifiers.
46
            if (MaterialField::byFieldIDAndMaterialID($query, $materialId, $fieldId, $materialField)) {
47
                //Getting first record
48
                $materialField = array_shift($materialField);
49
                //Set materialFieldId
50
                $this->materialFieldId = $materialField->id;
51
            }
52
        }
53
    }
54
55
    /**
56
     * Check on empty gallery. If materialFieldId = null and quantity images not more 1 then material not has images.
57
     *
58
     * @return boolean
59
     **/
60
    public function hasImages()
61
    {
62
        /**@var $hasImages */
63
        $hasImages = false;
64
65
        if (isset($this->materialFieldId)) {
66
            // Getting quantity images, if quantity more 0 then material has images
67
            if ($this->query
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\orm\QueryInterface as the method cond() does only exist in the following implementations of said interface: samson\activerecord\dbQuery.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
68
            ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
69
            ->cond(Field::F_DELETION, 1)
70
            ->cond(MaterialField::F_PRIMARY, $this->materialFieldId)
71
            ->count() > 0) {
72
                $hasImages = true;
73
            }
74
        }
75
        return $hasImages;
76
    }
77
78
    /**
79
     * Getting quantity images in additional field gallery
80
     *
81
     * @return integer $count
82
     */
83
    public function getCount()
84
    {
85
        /**@var integer $count quantity images in additional field gallery*/
86
        $count = 0;
87
88
        if ($this->hasImages()) {
89
            // Getting quantity images for gallery
90
            $count = $this->query
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\orm\QueryInterface as the method cond() does only exist in the following implementations of said interface: samson\activerecord\dbQuery.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
91
                ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
92
                ->cond(Field::F_DELETION, 1)
93
                ->cond(MaterialField::F_PRIMARY, $this->materialFieldId)
94
                ->count();
95
        }
96
97
        return $count;
98
    }
99
100
    /**
101
     * Get collection of images for material by gallery additional field selector. If none is passed
102
     * all images from gallery table would be returned empty array.
103
     *
104
     * @param integer $currentPage current page with images. Min value = 1
105
     * @param integer $countView quantity view by page
106
     * @return array
107
     */
108
    public function getImages($currentPage = null, $countView = 20)
109
    {
110
        /** @var $images[] Get material images for this gallery */
111
        $images = array();
112
113
        /** @var QueryInterface $query Database query interface*/
114
        $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...
115
116
        if ($this->hasImages()) {
117
            // Select all images in DB by materialFieldId
118
            $query = $this->query
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\orm\QueryInterface as the method cond() does only exist in the following implementations of said interface: samson\activerecord\dbQuery.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
119
                ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
120
                ->cond(Field::F_DELETION, 1)
121
                ->cond(MaterialField::F_PRIMARY, $this->materialFieldId);
122
123
            if (isset($currentPage) && $currentPage > 0) {
124
                //Set limit for query
125
                $query->limit(--$currentPage * $countView, $countView);
126
            }
127
128
            // Execute query
129
            $images = $query->exec();
130
        }
131
132
        return $images;
133
    }
134
}