Completed
Pull Request — master (#1352)
by Jordi Sala
02:42
created

ProviderDataTransformer::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
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 Sonata\MediaBundle\Form\DataTransformer;
13
14
use Psr\Log\LoggerAwareInterface;
15
use Psr\Log\LoggerAwareTrait;
16
use Psr\Log\NullLogger;
17
use Sonata\MediaBundle\Model\MediaInterface;
18
use Sonata\MediaBundle\Provider\Pool;
19
use Symfony\Component\Form\DataTransformerInterface;
20
21
class ProviderDataTransformer implements DataTransformerInterface, LoggerAwareInterface
22
{
23
    use LoggerAwareTrait;
24
25
    /**
26
     * @var Pool
27
     */
28
    protected $pool;
29
30
    /**
31
     * @var array
32
     */
33
    protected $options;
34
35
    /**
36
     * @param Pool   $pool
37
     * @param string $class
38
     * @param array  $options
39
     */
40
    public function __construct(Pool $pool, $class, array $options = [])
41
    {
42
        $this->pool = $pool;
43
        $this->options = $this->getOptions($options);
44
        $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...
45
46
        $this->logger = new NullLogger();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function transform($value)
53
    {
54
        if (null === $value) {
55
            return new $this->class();
56
        }
57
58
        return $value;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function reverseTransform($media)
65
    {
66
        if (!$media instanceof MediaInterface) {
67
            return $media;
68
        }
69
70
        $binaryContent = $media->getBinaryContent();
71
72
        // no binary
73
        if (empty($binaryContent)) {
74
            // and no media id
75
            if (null === $media->getId() && $this->options['empty_on_new']) {
76
                return;
77
            } elseif ($media->getId()) {
78
                return $media;
79
            }
80
81
            $media->setProviderStatus(MediaInterface::STATUS_PENDING);
82
            $media->setProviderReference(MediaInterface::MISSING_BINARY_REFERENCE);
83
84
            return $media;
85
        }
86
87
        // create a new media to avoid erasing other media or not ...
88
        $newMedia = $this->options['new_on_update'] ? new $this->class() : $media;
89
90
        $newMedia->setProviderName($media->getProviderName());
91
        $newMedia->setContext($media->getContext());
92
        $newMedia->setBinaryContent($binaryContent);
93
94
        if (!$newMedia->getProviderName() && $this->options['provider']) {
95
            $newMedia->setProviderName($this->options['provider']);
96
        }
97
98
        if (!$newMedia->getContext() && $this->options['context']) {
99
            $newMedia->setContext($this->options['context']);
100
        }
101
102
        $provider = $this->pool->getProvider($newMedia->getProviderName());
103
104
        try {
105
            $provider->transform($newMedia);
106
        } catch (\Exception $e) { // NEXT_MAJOR: When switching to PHP 7+, change this to \Throwable
107
            // #1107 We must never throw an exception here.
108
            // An exception here would prevent us to provide meaningful errors through the Form
109
            // Error message inspired from Monolog\ErrorHandler
110
            $this->logger->error(
111
                sprintf('Caught Exception %s: "%s" at %s line %s', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()),
112
                ['exception' => $e]
113
            );
114
        }
115
116
        return $newMedia;
117
    }
118
119
    /**
120
     * Define the default options for the DataTransformer.
121
     *
122
     * @param array $options
123
     *
124
     * @return array
125
     */
126
    protected function getOptions(array $options)
127
    {
128
        return array_merge([
129
            'provider' => false,
130
            'context' => false,
131
            'empty_on_new' => true,
132
            'new_on_update' => true,
133
        ], $options);
134
    }
135
}
136