vendor/specshaper/encrypt-bundle/Subscribers/EncryptEventSubscriber.php line 94

Open in your IDE?
  1. <?php
  2. namespace SpecShaper\EncryptBundle\Subscribers;
  3. use SpecShaper\EncryptBundle\Encryptors\EncryptorInterface;
  4. use SpecShaper\EncryptBundle\Event\EncryptEventInterface;
  5. use SpecShaper\EncryptBundle\Event\EncryptEvents;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. /**
  8.  * Doctrine event subscriber which encrypt/decrypt entities
  9.  */
  10. class EncryptEventSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * Encryptor created by the factory service.
  14.      *
  15.      * @var EncryptorInterface
  16.      */
  17.     protected $encryptor;
  18.     /**
  19.      * Store if the encryption is enabled or disabled in config.
  20.      *
  21.      * @var boolean
  22.      */
  23.     private $isDisabled;
  24.     /**
  25.      * EncryptSubscriber constructor.
  26.      *
  27.      * @param EncryptorInterface $encryptor
  28.      * @param                    $isDisabled
  29.      */
  30.     public function __construct(EncryptorInterface $encryptor$isDisabled)
  31.     {
  32.         $this->encryptor $encryptor;
  33.         $this->isDisabled $isDisabled;
  34.     }
  35.     /**
  36.      * Return the encryptor.
  37.      *
  38.      * @return \SpecShaper\EncryptBundle\Encryptors\EncryptorInterface
  39.      */
  40.     public function getEncryptor()
  41.     {
  42.         return $this->encryptor;
  43.     }
  44.     /**
  45.      * Realization of EventSubscriber interface method.
  46.      * @return array Return all events which this subscriber is listening
  47.      */
  48.     public static function getSubscribedEvents()
  49.     {
  50.         return array(
  51.             EncryptEvents::ENCRYPT => 'encrypt',
  52.             EncryptEvents::DECRYPT => 'decrypt',
  53.         );
  54.     }
  55.     /**
  56.      * Use an Encrypt even to encrypt a value.
  57.      *
  58.      * @param EncryptEventInterface $event
  59.      *
  60.      * @return EncryptEventInterface
  61.      */
  62.     public function encrypt(EncryptEventInterface $event){
  63.         $value $event->getValue();
  64.         if($this->isDisabled === false) {
  65.             $value $this->encryptor->encrypt($value);
  66.         }
  67.         $event->setValue($value);
  68.         return $event;
  69.     }
  70.     /**
  71.      * Use a decrypt event to decrypt a single value.
  72.      *
  73.      * @param EncryptEventInterface $event
  74.      *
  75.      * @return EncryptEventInterface
  76.      */
  77.     public function decrypt(EncryptEventInterface $event){
  78.         $value $event->getValue();
  79.         $decrypted $this->getEncryptor()->decrypt($value);
  80.         $event->setValue($decrypted);
  81.         return $event;
  82.     }
  83.     /**
  84.      * Decrypt a value.
  85.      *
  86.      * If the value is an object, or if it does not contain the suffic <ENC> then return the value iteslf back.
  87.      * Otherwise, decrypt the value and return.
  88.      *
  89.      * @param $value
  90.      *
  91.      * @return string
  92.      */
  93.     public function decryptValue($value){
  94.         // Else decrypt value and return.
  95.         $decrypted $this->getEncryptor()->decrypt($value);
  96.         return $decrypted;
  97.     }
  98. }