Test Failed
Push — master ( 9e6cf9...d006e3 )
by Petr
04:08
created

FileService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 31
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createBase64Image() 0 15 2
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 $filePath;
20
21
    public function __construct(string $applicationRootPath, ImageRepository $imageRepository)
22
    {
23
        $this->imageRepository = $imageRepository;
24
        $this->filePath = realpath($applicationRootPath . '/../var/upload');
25
    }
26
27
    public function createBase64Image(string $fileName, string $fileContents, $entity)
28
    {
29
        $image = new Image($fileName);
30
        $this->imageRepository->persist($image);
31
32
        $imagesPath = $this->filePath . '/images';
33
        $filePath = sprintf('%s%s%s', $imagesPath, DIRECTORY_SEPARATOR, $fileName);
34
        file_put_contents($filePath, base64_decode($fileContents));
35
36
        if ($entity instanceof Event) {
37
            $entity->addImage($image);
38
        }
39
40
        $this->imageRepository->flush();
41
    }
42
}
43