Passed
Push — master ( 60c6b0...f5065c )
by Petr
03:15
created

FileService::getExtensionFromBase64File()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace AppBundle\Service\File;
4
5
use AppBundle\Entity\Event;
6
use AppBundle\Entity\Image;
7
use AppBundle\Entity\Repository\ImageRepository;
8
9
/**
10
 * @author Vehsamrak
11
 */
12
class FileService
13
{
14
15
    /** @var ImageRepository */
16
    private $imageRepository;
17
18
    /** @var string */
19
    private $imagesPath;
20
21
    /** @var ImageExtensionChecker */
22
    private $extensionChecker;
23
24 6
    public function __construct(
25
        string $applicationRootPath,
26
        ImageRepository $imageRepository,
27
        ImageExtensionChecker $extensionChecker
28
    )
29
    {
30 6
        $this->imageRepository = $imageRepository;
31 6
        $this->imagesPath = $applicationRootPath . '/../var/upload/images';
32
33 6
        if (!is_dir($this->imagesPath)) {
34 1
            mkdir($this->imagesPath, 0755, true);
35
        }
36 6
        $this->extensionChecker = $extensionChecker;
37 6
    }
38
39 1
    public function createBase64Image(string $fileName, string $fileContents, $entity): Image
40
    {
41 1
        $image = new Image($fileName);
42 1
        $this->imageRepository->persist($image);
43
44 1
        $filePath = sprintf('%s%s%s', $this->imagesPath, DIRECTORY_SEPARATOR, $image->getName());
45 1
        file_put_contents($filePath, base64_decode($fileContents));
46
47 1
        if ($entity instanceof Event) {
48 1
            $entity->addImage($image);
49
        }
50
51 1
        $this->imageRepository->flush();
52
53 1
        return $image;
54
    }
55
56 1
    public function getExtensionFromBase64File($imageContent): string
57
    {
58 1
        return $this->extensionChecker->getExtensionFromBase64File($imageContent);
59
    }
60
}
61