Replicate   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 2
dl 0
loc 193
rs 9.68
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 26 5
A __construct() 0 6 1
A mtime() 0 4 1
A keys() 0 4 1
A exists() 0 4 1
B write() 0 27 6
A read() 0 4 1
A rename() 0 26 5
A supportsMetadata() 0 4 2
A setMetadata() 0 9 3
A getMetadata() 0 10 3
A getAdapterClassNames() 0 7 1
A createFile() 0 4 1
A createFileStream() 0 4 1
A listDirectory() 0 4 1
A isDirectory() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Filesystem;
15
16
use Gaufrette\Adapter as AdapterInterface;
17
use Gaufrette\Adapter\MetadataSupporter;
18
use Gaufrette\Filesystem;
19
use Psr\Log\LoggerInterface;
20
21
/**
22
 * @final since sonata-project/media-bundle 3.21.0
23
 */
24
class Replicate implements AdapterInterface, MetadataSupporter
25
{
26
    /**
27
     * @var AdapterInterface
28
     */
29
    protected $master;
30
31
    /**
32
     * @var AdapterInterface
33
     */
34
    protected $slave;
35
36
    /**
37
     * @var LoggerInterface
38
     */
39
    protected $logger;
40
41
    /**
42
     * @param LoggerInterface $logger
43
     */
44
    public function __construct(AdapterInterface $master, AdapterInterface $slave, ?LoggerInterface $logger = null)
45
    {
46
        $this->master = $master;
47
        $this->slave = $slave;
48
        $this->logger = $logger;
49
    }
50
51
    public function delete($key)
52
    {
53
        $ok = true;
54
55
        try {
56
            $this->slave->delete($key);
57
        } catch (\Exception $e) {
58
            if ($this->logger) {
59
                $this->logger->critical(sprintf('Unable to delete %s, error: %s', $key, $e->getMessage()));
60
            }
61
62
            $ok = false;
63
        }
64
65
        try {
66
            $this->master->delete($key);
67
        } catch (\Exception $e) {
68
            if ($this->logger) {
69
                $this->logger->critical(sprintf('Unable to delete %s, error: %s', $key, $e->getMessage()));
70
            }
71
72
            $ok = false;
73
        }
74
75
        return $ok;
76
    }
77
78
    public function mtime($key)
79
    {
80
        return $this->master->mtime($key);
81
    }
82
83
    public function keys()
84
    {
85
        return $this->master->keys();
86
    }
87
88
    public function exists($key)
89
    {
90
        return $this->master->exists($key);
91
    }
92
93
    public function write($key, $content, ?array $metadata = null)
94
    {
95
        $ok = true;
96
        $return = false;
97
98
        try {
99
            $return = $this->master->write($key, $content, $metadata);
0 ignored issues
show
Unused Code introduced by
The call to Adapter::write() has too many arguments starting with $metadata.

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...
100
        } catch (\Exception $e) {
101
            if ($this->logger) {
102
                $this->logger->critical(sprintf('Unable to write %s, error: %s', $key, $e->getMessage()));
103
            }
104
105
            $ok = false;
106
        }
107
108
        try {
109
            $return = $this->slave->write($key, $content, $metadata);
0 ignored issues
show
Unused Code introduced by
The call to Adapter::write() has too many arguments starting with $metadata.

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...
110
        } catch (\Exception $e) {
111
            if ($this->logger) {
112
                $this->logger->critical(sprintf('Unable to write %s, error: %s', $key, $e->getMessage()));
113
            }
114
115
            $ok = false;
116
        }
117
118
        return $ok && $return;
119
    }
120
121
    public function read($key)
122
    {
123
        return $this->master->read($key);
124
    }
125
126
    public function rename($key, $new)
127
    {
128
        $ok = true;
129
130
        try {
131
            $this->master->rename($key, $new);
132
        } catch (\Exception $e) {
133
            if ($this->logger) {
134
                $this->logger->critical(sprintf('Unable to rename %s, error: %s', $key, $e->getMessage()));
135
            }
136
137
            $ok = false;
138
        }
139
140
        try {
141
            $this->slave->rename($key, $new);
142
        } catch (\Exception $e) {
143
            if ($this->logger) {
144
                $this->logger->critical(sprintf('Unable to rename %s, error: %s', $key, $e->getMessage()));
145
            }
146
147
            $ok = false;
148
        }
149
150
        return $ok;
151
    }
152
153
    /**
154
     * If one of the adapters can allow inserting metadata.
155
     *
156
     * @return bool true if supports metadata, false if not
157
     */
158
    public function supportsMetadata()
159
    {
160
        return $this->master instanceof MetadataSupporter || $this->slave instanceof MetadataSupporter;
161
    }
162
163
    public function setMetadata($key, $metadata): void
164
    {
165
        if ($this->master instanceof MetadataSupporter) {
166
            $this->master->setMetadata($key, $metadata);
167
        }
168
        if ($this->slave instanceof MetadataSupporter) {
169
            $this->slave->setMetadata($key, $metadata);
170
        }
171
    }
172
173
    public function getMetadata($key)
174
    {
175
        if ($this->master instanceof MetadataSupporter) {
176
            return $this->master->getMetadata($key);
177
        } elseif ($this->slave instanceof MetadataSupporter) {
178
            return $this->slave->getMetadata($key);
179
        }
180
181
        return [];
182
    }
183
184
    /**
185
     * Gets the class names as an array for both adapters.
186
     *
187
     * @return string[]
188
     */
189
    public function getAdapterClassNames()
190
    {
191
        return [
192
            \get_class($this->master),
193
            \get_class($this->slave),
194
        ];
195
    }
196
197
    public function createFile($key, Filesystem $filesystem)
198
    {
199
        return $this->master->createFile($key, $filesystem);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Gaufrette\Adapter as the method createFile() does only exist in the following implementations of said interface: Gaufrette\Adapter\Ftp, Gaufrette\Adapter\PhpseclibSftp, Sonata\MediaBundle\Filesystem\Replicate.

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...
200
    }
201
202
    public function createFileStream($key, Filesystem $filesystem)
203
    {
204
        return $this->master->createFileStream($key, $filesystem);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Gaufrette\Adapter as the method createFileStream() does only exist in the following implementations of said interface: Sonata\MediaBundle\Filesystem\Replicate.

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...
205
    }
206
207
    public function listDirectory($directory = '')
208
    {
209
        return $this->master->listDirectory($directory);
0 ignored issues
show
Bug introduced by
The method listDirectory() does not exist on Gaufrette\Adapter. Did you maybe mean isDirectory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
210
    }
211
212
    public function isDirectory($key)
213
    {
214
        return $this->master->isDirectory($key);
215
    }
216
}
217