Skip to content

Commit fc8f494

Browse files
authored
Make File creation context configurable (base change) (#39)
* Make File creation context configurable (base change) * Added unit tests for CreationManager * Remove hardcoded time, timezone source
1 parent 501d3c3 commit fc8f494

File tree

16 files changed

+425
-46
lines changed

16 files changed

+425
-46
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
parameters:
22
google_openid_client_id: client_id
33
google_openid_client_secret: client_secret
4+
app.time_zone: 'UTC'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Enginewerk\ApplicationBundle\DateTime;
3+
4+
interface DateTimeReadInterface
5+
{
6+
/**
7+
* Returns DateTime object with current time
8+
*
9+
* @return \DateTime
10+
*/
11+
public function getCurrentDateTime();
12+
13+
/**
14+
* Returns DateTimeImmutable object with current time
15+
*
16+
* @return \DateTimeImmutable
17+
*/
18+
public function getCurrentDateTimeImmutable();
19+
}

emission/src/Enginewerk/ApplicationBundle/Resources/config/services.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
services:
2+
enginewerk_application.service.date_time_read_service:
3+
class: Enginewerk\ApplicationBundle\Service\DateTimeReadService
4+
public: true
5+
arguments:
6+
- '@enginewerk_application.service.data_time_zone'
7+
8+
enginewerk_application.service.data_time_zone:
9+
class: DateTimeZone
10+
public: false
11+
arguments:
12+
- '%app.time_zone%'
13+
214
enginewerk_application.logger.request_uuid_processor:
315
class: Enginewerk\ApplicationBundle\Logger\RequestUuidProcessor
416
public: true
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace Enginewerk\ApplicationBundle\Service;
3+
4+
use Enginewerk\ApplicationBundle\DateTime\DateTimeReadInterface;
5+
6+
final class DateTimeReadService implements DateTimeReadInterface
7+
{
8+
/**
9+
* @var \DateTimeZone
10+
*/
11+
private $dateTimeZone;
12+
13+
/**
14+
* @param \DateTimeZone $dateTimeZone
15+
*/
16+
public function __construct(\DateTimeZone $dateTimeZone)
17+
{
18+
$this->dateTimeZone = $dateTimeZone;
19+
}
20+
21+
/**
22+
* Returns DateTimeImmutable object with current time
23+
*
24+
* @return \DateTimeImmutable
25+
*/
26+
public function getCurrentDateTimeImmutable()
27+
{
28+
return new \DateTimeImmutable('NOW', $this->dateTimeZone);
29+
}
30+
31+
/**
32+
* Returns DateTime object with current time
33+
*
34+
* @return \DateTime
35+
*/
36+
public function getCurrentDateTime()
37+
{
38+
return new \DateTime('NOW', $this->dateTimeZone);
39+
}
40+
}

emission/src/Enginewerk/EmissionBundle/Entity/File.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace Enginewerk\EmissionBundle\Entity;
33

44
use Doctrine\Common\Collections\ArrayCollection;
5-
use Enginewerk\EmissionBundle\Generator\HashGenerator;
65
use Enginewerk\UserBundle\Entity\User;
76

87
class File
@@ -383,23 +382,4 @@ public function getUser()
383382
{
384383
return $this->user;
385384
}
386-
387-
public function preChange()
388-
{
389-
$currentTime = new \DateTime();
390-
$currentTime->setTimestamp(time());
391-
392-
if (null === $this->getFileHash()) {
393-
$fileHash = sha1(uniqid(mt_rand(), true));
394-
$this->fileHash = $fileHash;
395-
$this->setCreatedAt($currentTime);
396-
$this->setPublicIdentifier(HashGenerator::generateRandomHash(8));
397-
398-
$expirationTime = new \DateTime();
399-
$expirationTime->setTimestamp(time() + 86600);
400-
$this->setExpirationDate($expirationTime);
401-
}
402-
403-
$this->setUpdatedAt($currentTime);
404-
}
405385
}

emission/src/Enginewerk/EmissionBundle/EventListener/FileBlockSubscriber.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33

44
use Doctrine\Common\EventSubscriber;
55
use Doctrine\ORM\Event\LifecycleEventArgs;
6+
use Enginewerk\ApplicationBundle\DateTime\DateTimeReadInterface;
67
use Enginewerk\EmissionBundle\Entity\FileBlock;
78

89
class FileBlockSubscriber implements EventSubscriber
910
{
11+
/** @var DateTimeReadInterface */
12+
private $dateTimeReader;
13+
14+
/**
15+
* @param DateTimeReadInterface $dateTimeReader
16+
*/
17+
public function __construct(DateTimeReadInterface $dateTimeReader)
18+
{
19+
$this->dateTimeReader = $dateTimeReader;
20+
}
21+
1022
/**
1123
* @return string[]
1224
*/
@@ -25,9 +37,9 @@ public function prePersist(LifecycleEventArgs $args)
2537
$fileBlock = $args->getEntity();
2638

2739
if ($fileBlock instanceof FileBlock) {
28-
$createdAt = new \DateTime('now');
29-
$fileBlock->setUpdatedAt($createdAt);
30-
$fileBlock->setCreatedAt($createdAt);
40+
$currentTime = $this->dateTimeReader->getCurrentDateTime();
41+
$fileBlock->setUpdatedAt($currentTime);
42+
$fileBlock->setCreatedAt($currentTime);
3143
}
3244
}
3345
}

emission/src/Enginewerk/EmissionBundle/EventListener/FileSubscriber.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33

44
use Doctrine\Common\EventSubscriber;
55
use Doctrine\ORM\Event\LifecycleEventArgs;
6+
use Enginewerk\ApplicationBundle\DateTime\DateTimeReadInterface;
67
use Enginewerk\EmissionBundle\Entity\File as FileEntity;
78

89
class FileSubscriber implements EventSubscriber
910
{
11+
/** @var DateTimeReadInterface */
12+
private $dateTimeReader;
13+
14+
/**
15+
* @param DateTimeReadInterface $dateTimeReader
16+
*/
17+
public function __construct(DateTimeReadInterface $dateTimeReader)
18+
{
19+
$this->dateTimeReader = $dateTimeReader;
20+
}
21+
1022
/**
1123
* @return string[]
1224
*/
@@ -26,7 +38,9 @@ public function prePersist(LifecycleEventArgs $args)
2638
$file = $args->getEntity();
2739

2840
if ($file instanceof FileEntity) {
29-
$file->preChange();
41+
$currentTime = $this->dateTimeReader->getCurrentDateTime();
42+
$file->setCreatedAt($currentTime);
43+
$file->setUpdatedAt($currentTime);
3044
}
3145
}
3246

@@ -38,7 +52,7 @@ public function preUpdate(LifecycleEventArgs $args)
3852
$file = $args->getEntity();
3953

4054
if ($file instanceof FileEntity) {
41-
$file->preChange();
55+
$file->setUpdatedAt($this->dateTimeReader->getCurrentDateTime());
4256
}
4357
}
4458
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Enginewerk\EmissionBundle\Generator\Adapter;
3+
4+
use Enginewerk\EmissionBundle\Generator\HashGenerator;
5+
use Enginewerk\EmissionBundle\Generator\PublicIdentifierGeneratorInterface;
6+
7+
final class NativeGeneratorAdapter implements PublicIdentifierGeneratorInterface
8+
{
9+
/**
10+
* @inheritdoc
11+
*/
12+
public function generate($length = 8)
13+
{
14+
return HashGenerator::generateRandomHash($length);
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace Enginewerk\EmissionBundle\Generator;
3+
4+
interface PublicIdentifierGeneratorInterface
5+
{
6+
/**
7+
* @param int $length
8+
*
9+
* @return string
10+
*/
11+
public function generate($length = 8);
12+
}

emission/src/Enginewerk/EmissionBundle/Resources/config/services.yml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,28 @@ services:
1212
arguments:
1313
- '@enginewerk_emission.repository.file_repository'
1414
- '@enginewerk_emission.repository.file_block_repository'
15-
- '@enginewerk_user.repository_doctrine.user_repository'
15+
- '@enginewerk_emission.storage.manager.file_creation_manager'
1616
- '@enginewerk_fs.service.binary_storage_service'
1717

18+
enginewerk_emission.storage.manager.file_creation_manager:
19+
class: Enginewerk\EmissionBundle\Storage\Manager\FileCreationManager
20+
public: true
21+
arguments:
22+
- '@enginewerk_emission.repository.file_repository'
23+
- '@enginewerk_resumable.common.uuid.uuid_version4generator'
24+
- '@enginewerk_emission.generator_adapter.native_generator_adapter'
25+
- '@enginewerk_user.repository_doctrine.user_repository'
26+
- '@enginewerk_emission.storage_configuration.configuration'
27+
- '@enginewerk_application.service.date_time_read_service'
28+
29+
enginewerk_emission.storage_configuration.configuration:
30+
class: Enginewerk\EmissionBundle\Storage\Configuration\Configuration
31+
public: false
32+
33+
enginewerk_emission.generator_adapter.native_generator_adapter:
34+
class: Enginewerk\EmissionBundle\Generator\Adapter\NativeGeneratorAdapter
35+
public: false
36+
1837
enginewerk_emission.byte_formatter.script_fusion_adapter:
1938
public: false
2039
class: Enginewerk\Common\Formatter\ByteFormatter\Adapter\ScriptFusionAdapter
@@ -45,11 +64,13 @@ services:
4564
class: Enginewerk\EmissionBundle\EventListener\FileBlockSubscriber
4665
tags:
4766
- { name: doctrine.event_subscriber, connection: default }
67+
arguments: ['@enginewerk_application.service.date_time_read_service']
4868

4969
enginewerk_emission.event_listener.file_subscriber:
5070
class: Enginewerk\EmissionBundle\EventListener\FileSubscriber
5171
tags:
5272
- { name: doctrine.event_subscriber, connection: default }
73+
arguments: ['@enginewerk_application.service.date_time_read_service']
5374

5475
enginewerk_emission.service.file_write_service:
5576
public: false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace Enginewerk\EmissionBundle\Storage\Configuration;
3+
4+
final class Configuration implements FileCreateConfigurationInterface
5+
{
6+
/**
7+
* @inheritdoc
8+
*/
9+
public function getTimeToLive()
10+
{
11+
return 86600;
12+
}
13+
14+
/**
15+
* @inheritdoc
16+
*/
17+
public function getPublicIdentifierLength()
18+
{
19+
return 8;
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Enginewerk\EmissionBundle\Storage\Configuration;
3+
4+
interface FileCreateConfigurationInterface
5+
{
6+
/**
7+
* @return int seconds
8+
*/
9+
public function getTimeToLive();
10+
11+
/**
12+
* @return int Identifier length
13+
*/
14+
public function getPublicIdentifierLength();
15+
}

emission/src/Enginewerk/EmissionBundle/Storage/FileManager.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use Enginewerk\EmissionBundle\Entity\FileBlock;
77
use Enginewerk\EmissionBundle\Repository\FileBlockRepositoryInterface;
88
use Enginewerk\EmissionBundle\Repository\FileRepositoryInterface;
9+
use Enginewerk\EmissionBundle\Storage\Manager\CreateFileInterface;
910
use Enginewerk\FSBundle\Service\BinaryStorageInterface;
10-
use Enginewerk\UserBundle\Repository\UserFinderInterface;
1111

1212
final class FileManager implements FileCreationInterface
1313
{
@@ -17,27 +17,27 @@ final class FileManager implements FileCreationInterface
1717
/** @var FileBlockRepositoryInterface */
1818
private $fileBlockRepository;
1919

20-
/** @var UserFinderInterface */
21-
private $userRepository;
22-
2320
/** @var BinaryStorageInterface */
2421
private $binaryBlockStorage;
2522

23+
/** @var CreateFileInterface */
24+
private $fileCreationManager;
25+
2626
/**
2727
* @param FileRepositoryInterface $fileRepository
2828
* @param FileBlockRepositoryInterface $fileBlockRepository
29-
* @param UserFinderInterface $userRepository
29+
* @param CreateFileInterface $fileCreationManager
3030
* @param BinaryStorageInterface $binaryBlockStorage
3131
*/
3232
public function __construct(
3333
FileRepositoryInterface $fileRepository,
3434
FileBlockRepositoryInterface $fileBlockRepository,
35-
UserFinderInterface $userRepository,
35+
CreateFileInterface $fileCreationManager,
3636
BinaryStorageInterface $binaryBlockStorage
3737
) {
3838
$this->fileRepository = $fileRepository;
3939
$this->fileBlockRepository = $fileBlockRepository;
40-
$this->userRepository = $userRepository;
40+
$this->fileCreationManager = $fileCreationManager;
4141
$this->binaryBlockStorage = $binaryBlockStorage;
4242
}
4343

@@ -110,19 +110,7 @@ public function alterExpirationDate($publicIdentifier, \DateTimeInterface $expir
110110
*/
111111
public function createFile($fileName, $fileChecksum, $fileSize, $userIdentifier, $mimeType)
112112
{
113-
$user = $this->userRepository->getByEmail($userIdentifier);
114-
$file = new FileEntity();
115-
116-
$file->setName($fileName);
117-
$file->setChecksum($fileChecksum);
118-
$file->setSize($fileSize);
119-
$file->setType($mimeType);
120-
$file->setUser($user);
121-
$file->setComplete(false);
122-
123-
$this->fileRepository->persist($file);
124-
125-
return $file->getPublicIdentifier();
113+
return $this->fileCreationManager->createFile($fileName, $fileChecksum, $fileSize, $userIdentifier, $mimeType);
126114
}
127115

128116
/**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Enginewerk\EmissionBundle\Storage\Manager;
3+
4+
interface CreateFileInterface
5+
{
6+
/**
7+
* @param string $fileName
8+
* @param string $fileChecksum
9+
* @param int $fileSize
10+
* @param string $userIdentifier
11+
* @param string $mimeType
12+
*
13+
* @return string Public Identifier
14+
*/
15+
public function createFile($fileName, $fileChecksum, $fileSize, $userIdentifier, $mimeType);
16+
}

0 commit comments

Comments
 (0)