FileStorage::deleteFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php namespace Nord\Lumen\FileManager\Doctrine\ORM;
2
3
use Doctrine\ORM\EntityManagerInterface;
4
use Doctrine\ORM\EntityRepository;
5
use Nord\Lumen\FileManager\Contracts\File as FileContract;
6
use Nord\Lumen\FileManager\Contracts\FileStorage as FileStorageContract;
7
8 View Code Duplication
class FileStorage implements FileStorageContract
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
9
{
10
11
    /**
12
     * @var EntityManagerInterface
13
     */
14
    private $entityManager;
15
16
    /**
17
     * @var EntityRepository
18
     */
19
    private $repository;
20
21
22
    /**
23
     * FileStorage constructor.
24
     *
25
     * @param EntityManagerInterface $entityManager
26
     */
27
    public function __construct(EntityManagerInterface $entityManager)
28
    {
29
        $this->entityManager = $entityManager;
30
31
        $this->repository = $this->entityManager->getRepository(File::class);
32
    }
33
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function saveFile(FileContract $file)
39
    {
40
        $this->entityManager->persist($file);
41
        $this->entityManager->flush();
42
43
        return $file;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $file; (Nord\Lumen\FileManager\Contracts\File) is incompatible with the return type declared by the interface Nord\Lumen\FileManager\C...s\FileStorage::saveFile of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
44
    }
45
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function getFile($id)
51
    {
52
        return $this->repository->findOneBy(['id' => $id]);
53
    }
54
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function deleteFile($id)
60
    {
61
        $file = $this->getFile($id);
62
63
        if ($file === null) {
64
            return false;
65
        }
66
67
        $this->entityManager->remove($file);
68
        $this->entityManager->flush();
69
70
        return true;
71
    }
72
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function idExists($id)
78
    {
79
        return $this->getFile($id) !== null;
80
    }
81
}
82