LdapUserPhotoSubscriber   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 6 1
A syncUserPhoto() 0 12 4
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Bus\Subscriber;
13
14
use Jitamin\Bus\Event\UserProfileSyncEvent;
15
use Jitamin\Foundation\Identity\UserProfile;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
18
/**
19
 * Class LdapUserPhotoSubscriber.
20
 */
21
class LdapUserPhotoSubscriber extends BaseSubscriber implements EventSubscriberInterface
22
{
23
    /**
24
     * Get event listeners.
25
     *
26
     * @static
27
     *
28
     * @return array
29
     */
30
    public static function getSubscribedEvents()
31
    {
32
        return [
33
            UserProfile::EVENT_USER_PROFILE_AFTER_SYNC => 'syncUserPhoto',
34
        ];
35
    }
36
37
    /**
38
     * Save the user profile photo from LDAP to the object storage.
39
     *
40
     * @param UserProfileSyncEvent $event
41
     */
42
    public function syncUserPhoto(UserProfileSyncEvent $event)
43
    {
44
        if (is_a($event->getUser(), 'Jitamin\Services\Identity\LdapUserProvider')) {
45
            $profile = $event->getProfile();
46
            $photo = $event->getUser()->getPhoto();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Jitamin\Foundation\Identity\UserProviderInterface as the method getPhoto() does only exist in the following implementations of said interface: Jitamin\Services\Identity\LdapUserProvider.

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...
47
48
            if (empty($profile['avatar_path']) && !empty($photo)) {
49
                $this->logger->info('Saving user photo from LDAP profile');
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Bus\Subsc...dapUserPhotoSubscriber>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
50
                $this->avatarModel->uploadImageContent($profile['id'], $photo);
0 ignored issues
show
Documentation introduced by
The property avatarModel does not exist on object<Jitamin\Bus\Subsc...dapUserPhotoSubscriber>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
51
            }
52
        }
53
    }
54
}
55