Passed
Push — master ( 6a1b66...1520a2 )
by Przemysław eRIZ
04:33
created

setPathForNonImageMediaType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 10
c 1
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusCmsPlugin\Controller;
6
7
use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
8
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
9
use Sylius\Component\Resource\Model\ResourceInterface;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\HttpFoundation\File\File;
12
use Symfony\Component\HttpFoundation\Request;
13
use Webmozart\Assert\Assert;
14
15
trait ResourceDataProcessingTrait
16
{
17
    private function getResourceInterface(Request $request): object
18
    {
19
        return null !== $request->get('id') && $this->repository->find($request->get('id')) ?
20
            $this->repository->find($request->get('id')) :
21
            $this->factory->createNew();
22
    }
23
24
    private function getFormForResource(RequestConfiguration $configuration, ResourceInterface $resource): FormInterface
25
    {
26
        return $this->resourceFormFactory->create($configuration, $resource);
27
    }
28
29
    private function getRequestConfiguration(Request $request): RequestConfiguration
30
    {
31
        return $this->requestConfigurationFactory->create($this->metadata, $request);
32
    }
33
34
    private function setResourceMediaPath(MediaInterface $media): void
35
    {
36
        if (null === $media->getPath()) {
37
            return;
38
        }
39
        Assert::notNull($media->getMimeType());
40
        Assert::notNull($media->getType());
41
        if (1 === preg_match("/image\//", $media->getMimeType()) && 'image' === $media->getType()) {
0 ignored issues
show
Bug introduced by
It seems like $media->getMimeType() can also be of type null; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        if (1 === preg_match("/image\//", /** @scrutinizer ignore-type */ $media->getMimeType()) && 'image' === $media->getType()) {
Loading history...
42
            $this->setPathForImageMediaType($media);
43
        } else {
44
            $this->setPathForNonImageMediaType($media);
45
        }
46
    }
47
48
    private function setPathForImageMediaType(MediaInterface $media): void
49
    {
50
        Assert::string($media->getPath());
51
        if (!$this->cacheManager->isStored($media->getPath(), $this::FILTER)) {
0 ignored issues
show
Bug introduced by
The constant BitBag\SyliusCmsPlugin\C...ProcessingTrait::FILTER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
52
            $this->cacheManager->store($this->dataManager->find($this::FILTER, $media->getPath()), $media->getPath(), $this::FILTER);
53
        }
54
        $resolvedPath = $this->cacheManager->resolve($media->getPath(), $this::FILTER);
55
        $fileContents = file_get_contents($resolvedPath);
56
        Assert::string($fileContents);
57
        $this->setFileContentsAsMediaPath($media, $fileContents);
58
    }
59
60
    private function setPathForNonImageMediaType(MediaInterface $media): void
61
    {
62
        $mediaPath = $this->getMediaPathIfNotNull($media);
63
        $file = new File($mediaPath);
64
        $fileContents = file_get_contents($file->getPathname());
65
        Assert::string($fileContents);
66
        $this->setFileContentsAsMediaPath($media, $fileContents);
67
    }
68
69
    private function setFileContentsAsMediaPath(MediaInterface $media, string $fileContents): void
70
    {
71
        $base64Content = base64_encode($fileContents);
72
        $path = 'data:' . $media->getMimeType() . ';base64, ' . $base64Content;
73
        $media->setPath($path);
74
    }
75
76
    private function getMediaPathIfNotNull(MediaInterface $media): string
77
    {
78
        Assert::string($media->getPath());
79
        Assert::string($this->getParameter('sylius_core.public_dir'));
0 ignored issues
show
Bug introduced by
It seems like getParameter() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        Assert::string($this->/** @scrutinizer ignore-call */ getParameter('sylius_core.public_dir'));
Loading history...
80
81
        return $this->getParameter('sylius_core.public_dir') . $media->getPath();
82
    }
83
}
84