Completed
Push — master ( 3a69b5...ff1cb1 )
by Aimeos
03:21
created

FlyReplicate::getProvider()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package MW
7
 * @subpackage Filesystem
8
 */
9
10
11
namespace Aimeos\MW\Filesystem;
12
13
use League\Flysystem\Filesystem;
14
use League\Flysystem\Replicate\ReplicateAdapter;
15
16
17
/**
18
 * Implementation of Flysystem Replicate file system adapter
19
 *
20
 * @package MW
21
 * @subpackage Filesystem
22
 */
23
class FlyReplicate extends FlyBase implements Iface, DirIface, MetaIface
24
{
25
	private $fs;
26
27
28
	/**
29
	 * Returns the file system provider
30
	 *
31
	 * @return \League\Flysystem\FilesystemInterface File system provider
32
	 */
33
	protected function getProvider()
34
	{
35
		if( !isset( $this->fs ) )
36
		{
37
			$config = $this->getConfig();
38
39
			if( !isset( $config['source'] ) ) {
40
				throw new Exception( sprintf( 'Configuration option "%1$s" missing', 'source' ) );
41
			}
42
43
			if( !isset( $config['replica'] ) ) {
44
				throw new Exception( sprintf( 'Configuration option "%1$s" missing', 'replica' ) );
45
			}
46
47
			$source = Factory::create( (array) $config['source'] )->getAdapter();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MW\Filesystem\Iface as the method getAdapter() does only exist in the following implementations of said interface: Aimeos\MW\Filesystem\FlyAwsS3, Aimeos\MW\Filesystem\FlyAzure, Aimeos\MW\Filesystem\FlyBase, Aimeos\MW\Filesystem\FlyDropbox, Aimeos\MW\Filesystem\FlyFtp, Aimeos\MW\Filesystem\FlyGridfs, Aimeos\MW\Filesystem\FlyLocal, Aimeos\MW\Filesystem\FlyMemory, Aimeos\MW\Filesystem\FlyNone, Aimeos\MW\Filesystem\FlyPhpcr, Aimeos\MW\Filesystem\FlyRackspace, Aimeos\MW\Filesystem\FlyReplicate, Aimeos\MW\Filesystem\FlySftp, Aimeos\MW\Filesystem\FlyWebdav, Aimeos\MW\Filesystem\FlyZip.

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...
48
			$replica = Factory::create( (array) $config['replica'] )->getAdapter();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MW\Filesystem\Iface as the method getAdapter() does only exist in the following implementations of said interface: Aimeos\MW\Filesystem\FlyAwsS3, Aimeos\MW\Filesystem\FlyAzure, Aimeos\MW\Filesystem\FlyBase, Aimeos\MW\Filesystem\FlyDropbox, Aimeos\MW\Filesystem\FlyFtp, Aimeos\MW\Filesystem\FlyGridfs, Aimeos\MW\Filesystem\FlyLocal, Aimeos\MW\Filesystem\FlyMemory, Aimeos\MW\Filesystem\FlyNone, Aimeos\MW\Filesystem\FlyPhpcr, Aimeos\MW\Filesystem\FlyRackspace, Aimeos\MW\Filesystem\FlyReplicate, Aimeos\MW\Filesystem\FlySftp, Aimeos\MW\Filesystem\FlyWebdav, Aimeos\MW\Filesystem\FlyZip.

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...
49
			$this->fs = new Filesystem( new ReplicateAdapter( $source, $replica ) );
50
		}
51
52
		return $this->fs;
53
	}
54
}
55