ProviderDataTransformer::reverseTransform()   C
last analyzed

Complexity

Conditions 12
Paths 20

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 6.9666
c 0
b 0
f 0
cc 12
nc 20
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Form\DataTransformer;
15
16
use Psr\Log\LoggerAwareInterface;
17
use Psr\Log\LoggerAwareTrait;
18
use Psr\Log\NullLogger;
19
use Sonata\MediaBundle\Model\MediaInterface;
20
use Sonata\MediaBundle\Provider\Pool;
21
use Symfony\Component\Form\DataTransformerInterface;
22
23
/**
24
 * @final since sonata-project/media-bundle 3.21.0
25
 */
26
class ProviderDataTransformer implements DataTransformerInterface, LoggerAwareInterface
27
{
28
    use LoggerAwareTrait;
29
30
    /**
31
     * @var Pool
32
     */
33
    protected $pool;
34
35
    /**
36
     * @var array
37
     */
38
    protected $options;
39
40
    /**
41
     * @param string $class
42
     */
43
    public function __construct(Pool $pool, $class, array $options = [])
44
    {
45
        $this->pool = $pool;
46
        $this->options = $this->getOptions($options);
47
        $this->class = $class;
0 ignored issues
show
Bug introduced by
The property class does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
49
        $this->logger = new NullLogger();
50
    }
51
52
    public function transform($value)
53
    {
54
        if (null === $value) {
55
            return new $this->class();
56
        }
57
58
        return $value;
59
    }
60
61
    public function reverseTransform($media)
62
    {
63
        if (!$media instanceof MediaInterface) {
64
            return $media;
65
        }
66
67
        $binaryContent = $media->getBinaryContent();
68
69
        // no binary
70
        if (empty($binaryContent)) {
71
            // and no media id
72
            if (null === $media->getId() && $this->options['empty_on_new']) {
73
                return;
74
            } elseif ($media->getId()) {
75
                return $media;
76
            }
77
78
            $media->setProviderStatus(MediaInterface::STATUS_PENDING);
79
            $media->setProviderReference(MediaInterface::MISSING_BINARY_REFERENCE);
80
81
            return $media;
82
        }
83
84
        // create a new media to avoid erasing other media or not ...
85
        $newMedia = $this->options['new_on_update'] ? new $this->class() : $media;
86
87
        $newMedia->setProviderName($media->getProviderName());
88
        $newMedia->setContext($media->getContext());
89
        $newMedia->setBinaryContent($binaryContent);
90
91
        if (!$newMedia->getProviderName() && $this->options['provider']) {
92
            $newMedia->setProviderName($this->options['provider']);
93
        }
94
95
        if (!$newMedia->getContext() && $this->options['context']) {
96
            $newMedia->setContext($this->options['context']);
97
        }
98
99
        $provider = $this->pool->getProvider($newMedia->getProviderName());
100
101
        try {
102
            $provider->transform($newMedia);
103
        } catch (\Throwable $e) {
104
            // #1107 We must never throw an exception here.
105
            // An exception here would prevent us to provide meaningful errors through the Form
106
            // Error message inspired from Monolog\ErrorHandler
107
            $this->logger->error(
108
                sprintf('Caught Exception %s: "%s" at %s line %s', \get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()),
109
                ['exception' => $e]
110
            );
111
        }
112
113
        return $newMedia;
114
    }
115
116
    /**
117
     * Define the default options for the DataTransformer.
118
     *
119
     * @return array
120
     */
121
    protected function getOptions(array $options)
122
    {
123
        return array_merge([
124
            'provider' => false,
125
            'context' => false,
126
            'empty_on_new' => true,
127
            'new_on_update' => true,
128
        ], $options);
129
    }
130
}
131