@@ 8-81 (lines=74) @@ | ||
5 | use Nord\Lumen\FileManager\Contracts\File as FileContract; |
|
6 | use Nord\Lumen\FileManager\Contracts\FileStorage as FileStorageContract; |
|
7 | ||
8 | class FileStorage implements FileStorageContract |
|
9 | { |
|
10 | ||
11 | /** |
|
12 | * @var DocumentManager |
|
13 | */ |
|
14 | private $documentManager; |
|
15 | ||
16 | /** |
|
17 | * @var DocumentRepository |
|
18 | */ |
|
19 | private $repository; |
|
20 | ||
21 | ||
22 | /** |
|
23 | * FileStorage constructor. |
|
24 | * |
|
25 | * @param DocumentManager $documentManager |
|
26 | */ |
|
27 | public function __construct(DocumentManager $documentManager) |
|
28 | { |
|
29 | $this->documentManager = $documentManager; |
|
30 | ||
31 | $this->repository = $this->documentManager->getRepository(File::class); |
|
32 | } |
|
33 | ||
34 | ||
35 | /** |
|
36 | * @inheritdoc |
|
37 | */ |
|
38 | public function saveFile(FileContract $file) |
|
39 | { |
|
40 | $this->documentManager->persist($file); |
|
41 | $this->documentManager->flush(); |
|
42 | ||
43 | return $file; |
|
44 | } |
|
45 | ||
46 | ||
47 | /** |
|
48 | * @inheritdoc |
|
49 | */ |
|
50 | public function getFile($id) |
|
51 | { |
|
52 | return $this->repository->findOneBy(['fileId' => $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->documentManager->remove($file); |
|
68 | $this->documentManager->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 |
@@ 8-81 (lines=74) @@ | ||
5 | use Nord\Lumen\FileManager\Contracts\File as FileContract; |
|
6 | use Nord\Lumen\FileManager\Contracts\FileStorage as FileStorageContract; |
|
7 | ||
8 | class FileStorage implements FileStorageContract |
|
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; |
|
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 |