| @@ 28-79 (lines=52) @@ | ||
| 25 | use Surfnet\StepupGateway\U2fVerificationBundle\Exception\LogicException; |
|
| 26 | use Surfnet\StepupGateway\U2fVerificationBundle\Value\KeyHandle; |
|
| 27 | ||
| 28 | class KeyHandleType extends Type |
|
| 29 | { |
|
| 30 | const NAME = 'u2f_key_handle'; |
|
| 31 | ||
| 32 | public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
|
| 33 | { |
|
| 34 | return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration); |
|
| 35 | } |
|
| 36 | ||
| 37 | public function convertToDatabaseValue($value, AbstractPlatform $platform) |
|
| 38 | { |
|
| 39 | if ($value instanceof KeyHandle) { |
|
| 40 | return $value->getKeyHandle(); |
|
| 41 | } |
|
| 42 | ||
| 43 | if ($value === null) { |
|
| 44 | return null; |
|
| 45 | } |
|
| 46 | ||
| 47 | throw new LogicException( |
|
| 48 | sprintf( |
|
| 49 | 'PHP value should be instance of KeyHandle or NULL, got "%s"', |
|
| 50 | is_object($value) ? get_class($value) : gettype($value) |
|
| 51 | ) |
|
| 52 | ); |
|
| 53 | } |
|
| 54 | ||
| 55 | public function convertToPHPValue($value, AbstractPlatform $platform) |
|
| 56 | { |
|
| 57 | if ($value === null) { |
|
| 58 | return $value; |
|
| 59 | } |
|
| 60 | ||
| 61 | try { |
|
| 62 | return new KeyHandle($value); |
|
| 63 | } catch (InvalidArgumentException $e) { |
|
| 64 | // Get a nice standard message to throw, keeping the exception chain. |
|
| 65 | $doctrineExceptionMessage = ConversionException::conversionFailedFormat( |
|
| 66 | $value, |
|
| 67 | $this->getName(), |
|
| 68 | $platform->getDateTimeFormatString() |
|
| 69 | )->getMessage(); |
|
| 70 | ||
| 71 | throw new ConversionException($doctrineExceptionMessage, 0, $e); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||
| 75 | public function getName() |
|
| 76 | { |
|
| 77 | return self::NAME; |
|
| 78 | } |
|
| 79 | } |
|
| 80 | ||
| @@ 28-79 (lines=52) @@ | ||
| 25 | use Surfnet\StepupGateway\U2fVerificationBundle\Exception\LogicException; |
|
| 26 | use Surfnet\StepupGateway\U2fVerificationBundle\Value\PublicKey; |
|
| 27 | ||
| 28 | class PublicKeyType extends Type |
|
| 29 | { |
|
| 30 | const NAME = 'u2f_public_key'; |
|
| 31 | ||
| 32 | public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
|
| 33 | { |
|
| 34 | return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration); |
|
| 35 | } |
|
| 36 | ||
| 37 | public function convertToDatabaseValue($value, AbstractPlatform $platform) |
|
| 38 | { |
|
| 39 | if ($value instanceof PublicKey) { |
|
| 40 | return $value->getPublicKey(); |
|
| 41 | } |
|
| 42 | ||
| 43 | if ($value === null) { |
|
| 44 | return null; |
|
| 45 | } |
|
| 46 | ||
| 47 | throw new LogicException( |
|
| 48 | sprintf( |
|
| 49 | 'PHP value should be instance of PublicKey or NULL, got "%s"', |
|
| 50 | is_object($value) ? get_class($value) : gettype($value) |
|
| 51 | ) |
|
| 52 | ); |
|
| 53 | } |
|
| 54 | ||
| 55 | public function convertToPHPValue($value, AbstractPlatform $platform) |
|
| 56 | { |
|
| 57 | if ($value === null) { |
|
| 58 | return $value; |
|
| 59 | } |
|
| 60 | ||
| 61 | try { |
|
| 62 | return new PublicKey($value); |
|
| 63 | } catch (InvalidArgumentException $e) { |
|
| 64 | // Get a nice standard message to throw, keeping the exception chain. |
|
| 65 | $doctrineExceptionMessage = ConversionException::conversionFailedFormat( |
|
| 66 | $value, |
|
| 67 | $this->getName(), |
|
| 68 | $platform->getDateTimeFormatString() |
|
| 69 | )->getMessage(); |
|
| 70 | ||
| 71 | throw new ConversionException($doctrineExceptionMessage, 0, $e); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||
| 75 | public function getName() |
|
| 76 | { |
|
| 77 | return self::NAME; |
|
| 78 | } |
|
| 79 | } |
|
| 80 | ||