Completed
Push — master ( 5afb5d...6dae66 )
by dima
05:53
created

UserRepository::save()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace Core\Models\User;
4
5
use Propel\Runtime\ActiveRecord\ActiveRecordInterface;
6
7
use Core\Models\User\User;
8
use Core\Models\User\UserQuery;
9
10
/**
11
 * Description of UserRepo
12
 *
13
 * @author Dmitriy
14
 */
15
class UserRepository implements \Frameworkless\CrudInterface
16
{   
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
17
    
18
    public function find(array $conditions = [])
19
    {
20
        return UserQuery::create()->findOneByArray($conditions);  
21
    }
22
23
    /**
24
     * Поиск по id
25
     * @param type $id
26
     * @return \Models\User\User $User
27
     * @throws \InvalidArgumentException
28
     */
29
    public function findById($id)
30
    {
31
        if (!$User = UserQuery::create()->findPk($id)) {
32
            throw new \InvalidArgumentException(sprintf('User with ID %d does not exist',$id));
33
        }
34
35
        return $User;
36
    }
37
38
    public function findMany(array $conditions = [])
39
    {
40
        return UserQuery::create()->findByArray($conditions);    
41
    }
42
43
    public function delete(ActiveRecordInterface $Model)
44
    {
45
        
46
    }
47
48
    /**
49
     * Сохраняем
50
     * @param ActiveRecordInterface $Model
51
     * @return boolean
52
     * @throws \DomainException
53
     */
54
    public function save(ActiveRecordInterface $Model)
55
    {
56
        if (!$Model->validate()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Propel\Runtime\ActiveRecord\ActiveRecordInterface as the method validate() does only exist in the following implementations of said interface: Core\Models\User\Base\User, Core\Models\User\User.

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...
57
            
58
            throw new \Frameworkless\Exceptions\ValidateException("User not validate", $Model->getValidationFailures());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Propel\Runtime\ActiveRecord\ActiveRecordInterface as the method getValidationFailures() does only exist in the following implementations of said interface: Core\Models\User\Base\User, Core\Models\User\User.

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...
Unused Code introduced by
The call to ValidateException::__construct() has too many arguments starting with 'User not validate'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
59
        }        
60
        
61
        
62
        if (!$Model->save()) {
63
            throw new \DomainException(sprintf('User not save',$id));
64
        }
65
66
        return true;        
67
    }
68
    
69
    /**
70
     * 
71
     * @return \Models\User\Models\User\User $User
72
     */
73
    public function build(){
74
        return new User;
75
    }
76
77
}
78