Skip to content

Commit

Permalink
Merge pull request #61 from flavou45/master
Browse files Browse the repository at this point in the history
Symfony 6
  • Loading branch information
absolute-quantum authored Feb 8, 2022
2 parents bc5c091 + dfeb64e commit 4146d88
Show file tree
Hide file tree
Showing 20 changed files with 308 additions and 62 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryDefuseTest::testPersistEntity":4,"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryDefuseTest::testNoUpdateOnReadEncrypted":4,"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryDefuseTest::testStoredDataIsEncrypted":4,"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryHaliteTest::testPersistEntity":1,"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryHaliteTest::testNoUpdateOnReadEncrypted":1,"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryHaliteTest::testStoredDataIsEncrypted":1,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\DependencyInjection\\DoctrineEncryptExtensionTest::testConfigLoadCustom":1,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Encryptors\\HaliteEncryptorTest::testEncryptWithoutExtensionThrowsException":1,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Encryptors\\DefuseEncryptorTest::testEncrypt":4,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Encryptors\\DefuseEncryptorTest::testGenerateKey":4,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Encryptors\\HaliteEncryptorTest::testEncryptExtension":1,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Encryptors\\HaliteEncryptorTest::testGenerateKey":1,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testProcessFieldsNoEncryptor":4},"times":{"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryDefuseTest::testPersistEntity":0.456,"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryDefuseTest::testNoUpdateOnReadEncrypted":1.046,"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryDefuseTest::testStoredDataIsEncrypted":0.654,"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryHaliteTest::testPersistEntity":0.106,"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryHaliteTest::testNoUpdateOnReadEncrypted":0.142,"Ambta\\DoctrineEncryptBundle\\Tests\\Functional\\BasicQueryTest\\BasicQueryHaliteTest::testStoredDataIsEncrypted":0.126,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\DependencyInjection\\DoctrineEncryptExtensionTest::testConfigLoadHalite":0.031,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\DependencyInjection\\DoctrineEncryptExtensionTest::testConfigLoadDefuse":0.012,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\DependencyInjection\\DoctrineEncryptExtensionTest::testConfigLoadCustom":0.013,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Encryptors\\DefuseEncryptorTest::testEncrypt":0.252,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Encryptors\\DefuseEncryptorTest::testGenerateKey":0.132,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Encryptors\\HaliteEncryptorTest::testEncryptExtension":0.005,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Encryptors\\HaliteEncryptorTest::testGenerateKey":0.008,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Encryptors\\HaliteEncryptorTest::testEncryptWithoutExtensionThrowsException":0.001,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testSetRestorEncryptor":0.01,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testProcessFieldsEncrypt":0.003,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testProcessFieldsEncryptExtend":0.003,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testProcessFieldsEncryptEmbedded":0.004,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testProcessFieldsEncryptNull":0.002,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testProcessFieldsNoEncryptor":0.001,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testProcessFieldsDecrypt":0.002,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testProcessFieldsDecryptExtended":0.003,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testProcessFieldsDecryptEmbedded":0.003,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testProcessFieldsDecryptNull":0.002,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testProcessFieldsDecryptNonEncrypted":0.002,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testOnFlush":0.052,"Ambta\\DoctrineEncryptBundle\\Tests\\Unit\\Subscribers\\DoctrineEncryptSubscriberTest::testPostFlush":0.006}}
1 change: 1 addition & 0 deletions Tests/Functional/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function setUp(): void

public function tearDown(): void
{
$this->entityManager->getConnection()->close();
unlink($this->dbFile);
}

Expand Down
17 changes: 11 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"license": "MIT",
"description": "Encrypted symfony entity's by verified and standardized libraries",
"require": {
"php": "^7.2|^8.0",
"php": "^8.0",
"paragonie/halite": "^4.6",
"paragonie/sodium_compat": "^1.5",
"doctrine/orm": "^2.5",
"symfony/property-access": "^4.1|^5.0",
"symfony/dependency-injection": "^4.1|^5.0",
"symfony/yaml": "^4.1|^5.0",
"symfony/http-kernel": "^4.1|^5.0",
"symfony/config": "^4.1|^5.0"
"symfony/property-access": "^4.1|^5.0|^6.0",
"symfony/dependency-injection": "^4.1|^5.0|^6.0",
"symfony/yaml": "^4.1|^5.0|^6.0",
"symfony/http-kernel": "^4.1|^5.0|^6.0",
"symfony/config": "^4.1|^5.0|^6.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0|^9.0",
Expand All @@ -32,5 +32,10 @@
"psr-4": {
"Ambta\\DoctrineEncryptBundle\\Tests\\": "Tests/"
}
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true
}
}
}
5 changes: 4 additions & 1 deletion src/AmbtaDoctrineEncryptBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Ambta\DoctrineEncryptBundle;

use JetBrains\PhpStorm\Pure;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension;

class AmbtaDoctrineEncryptBundle extends Bundle
{
public function getContainerExtension()
#[Pure]
public function getContainerExtension(): ?ExtensionInterface
{
return new DoctrineEncryptExtension();
}
Expand Down
19 changes: 9 additions & 10 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ abstract class AbstractCommand extends Command
/**
* @var EntityManagerInterface
*/
protected $entityManager;
protected EntityManagerInterface|EntityManager $entityManager;

/**
* @var DoctrineEncryptSubscriber
*/
protected $subscriber;
protected DoctrineEncryptSubscriber $subscriber;

/**
* @var Reader
*/
protected $annotationReader;
protected Reader $annotationReader;

/**
* AbstractCommand constructor.
Expand All @@ -52,14 +52,13 @@ public function __construct(
* Get an result iterator over the whole table of an entity.
*
* @param string $entityName
*
* @return \Doctrine\ORM\Internal\Hydration\IterableResult
* @return iterable|array
*/
protected function getEntityIterator($entityName)
protected function getEntityIterator(string $entityName): iterable
{
$query = $this->entityManager->createQuery(sprintf('SELECT o FROM %s o', $entityName));

return $query->iterate();
return $query->toIterable();
}

/**
Expand All @@ -69,7 +68,7 @@ protected function getEntityIterator($entityName)
*
* @return int
*/
protected function getTableCount($entityName)
protected function getTableCount(string $entityName): int
{
$query = $this->entityManager->createQuery(sprintf('SELECT COUNT(o) FROM %s o', $entityName));

Expand All @@ -82,7 +81,7 @@ protected function getTableCount($entityName)
*
* @return array
*/
protected function getEncryptionableEntityMetaData()
protected function getEncryptionableEntityMetaData(): array
{
$validMetaData = [];
$metaDataArray = $this->entityManager->getMetadataFactory()->getAllMetadata();
Expand All @@ -109,7 +108,7 @@ protected function getEncryptionableEntityMetaData()
*
* @return array
*/
protected function getEncryptionableProperties($entityMetaData)
protected function getEncryptionableProperties($entityMetaData): array
{
//Create reflectionClass for each meta data object
$reflectionClass = new \ReflectionClass($entityMetaData->name);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/DoctrineDecryptDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
// Get entity manager, question helper, subscriber service and annotation reader
$question = $this->getHelper('question');
Expand Down
4 changes: 2 additions & 2 deletions src/Command/DoctrineEncryptDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
// Get entity manager, question helper, subscriber service and annotation reader
$question = $this->getHelper('question');
Expand Down Expand Up @@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln(sprintf('Processing <comment>%s</comment>', $metaData->name));
$progressBar = new ProgressBar($output, $totalCount);
foreach ($iterator as $row) {
$this->subscriber->processFields($row[0]);
$this->subscriber->processFields((is_array($row) ? $row[0] : $row));

if (($i % $batchSize) === 0) {
$this->entityManager->flush();
Expand Down
2 changes: 1 addition & 1 deletion src/Command/DoctrineEncryptStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$metaDataArray = $this->entityManager->getMetadataFactory()->getAllMetadata();

Expand Down
12 changes: 12 additions & 0 deletions src/Configuration/Annotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Ambta\DoctrineEncryptBundle\Configuration;

/**
* @internal
*/
interface Annotation
{
}
6 changes: 5 additions & 1 deletion src/Configuration/Encrypted.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Ambta\DoctrineEncryptBundle\Configuration;

use Attribute;

/**
* The Encrypted class handles the @Encrypted annotation.
*
* @author Victor Melnik <melnikvictorl@gmail.com>
* @Annotation
* @Target("PROPERTY")
*/
class Encrypted
#[Attribute(Attribute::TARGET_PROPERTY)]
class Encrypted implements Annotation
{
// Placeholder
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Configuration implements ConfigurationInterface
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
// Create tree builder
$treeBuilder = new TreeBuilder('ambta_doctrine_encrypt');
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/DoctrineEncryptExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function load(array $configs, ContainerBuilder $container)
*
* @return string
*/
public function getAlias()
public function getAlias(): string
{
return 'ambta_doctrine_encrypt';
}
Expand Down
12 changes: 6 additions & 6 deletions src/Encryptors/DefuseEncryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

class DefuseEncryptor implements EncryptorInterface
{
private $fs;
private $encryptionKey;
private $keyFile;
private Filesystem $fs;
private ?string $encryptionKey = null;
private string $keyFile;

/**
* {@inheritdoc}
Expand All @@ -28,20 +28,20 @@ public function __construct(string $keyFile)
/**
* {@inheritdoc}
*/
public function encrypt($data)
public function encrypt(string $data): string
{
return \Defuse\Crypto\Crypto::encryptWithPassword($data, $this->getKey());
}

/**
* {@inheritdoc}
*/
public function decrypt($data)
public function decrypt(string $data): string
{
return \Defuse\Crypto\Crypto::decryptWithPassword($data, $this->getKey());
}

private function getKey()
private function getKey(): string
{
if ($this->encryptionKey === null) {
if ($this->fs->exists($this->keyFile)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Encryptors/EncryptorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ interface EncryptorInterface
* @param string $data Plain text to encrypt
* @return string Encrypted text
*/
public function encrypt($data);
public function encrypt(string $data): string;

/**
* @param string $data Encrypted text
* @return string Plain text
*/
public function decrypt($data);
public function decrypt(string $data): string;
}
11 changes: 6 additions & 5 deletions src/Encryptors/HaliteEncryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ambta\DoctrineEncryptBundle\Encryptors;

use ParagonIE\Halite\Alerts\CannotPerformOperation;
use ParagonIE\Halite\Symmetric\EncryptionKey;
use ParagonIE\HiddenString\HiddenString;
use ParagonIE\Halite\KeyFactory;
use ParagonIE\Halite\Symmetric\Crypto;
Expand All @@ -15,8 +16,8 @@

class HaliteEncryptor implements EncryptorInterface
{
private $encryptionKey;
private $keyFile;
private ?EncryptionKey $encryptionKey = null;
private string $keyFile;

/**
* {@inheritdoc}
Expand All @@ -29,15 +30,15 @@ public function __construct(string $keyFile)
/**
* {@inheritdoc}
*/
public function encrypt($data)
public function encrypt(string $data): string
{
return Crypto::encrypt(new HiddenString($data), $this->getKey());
}

/**
* {@inheritdoc}
*/
public function decrypt($data)
public function decrypt(string $data): string
{
$data = Crypto::decrypt($data, $this->getKey());

Expand All @@ -49,7 +50,7 @@ public function decrypt($data)
return $data;
}

private function getKey()
private function getKey(): EncryptionKey
{
if ($this->encryptionKey === null) {
try {
Expand Down
Loading

0 comments on commit 4146d88

Please sign in to comment.