PayU integration by Krzysztof Gzocha
  • Namespace
  • Class

Namespaces

  • Team3
    • PayU
      • Annotation
      • Communication
        • CurlRequestBuilder
        • HttpStatusParser
        • Notification
        • Process
          • NotificationProcess
          • ResponseDeserializer
        • Request
          • Model
        • Response
          • Model
        • Sender
      • Configuration
        • Credentials
      • Order
        • Autocomplete
          • Strategy
        • Model
          • Buyer
          • Money
          • Products
          • ShippingMethods
          • Traits
        • Transformer
          • UserOrder
            • Strategy
              • Product
              • ShippingMethod
      • PropertyExtractor
        • Reader
      • Serializer
      • SignatureCalculator
        • Encoder
          • Algorithms
          • Strategy
        • ParametersSorter
        • Validator
      • ValidatorBuilder

Classes

  • Team3\PayU\ValidatorBuilder\ValidatorBuilder

Interfaces

  • Team3\PayU\ValidatorBuilder\ValidatorBuilderInterface
  1 <?php
  2 /**
  3  * @author Krzysztof Gzocha <krzysztof.gzocha@xsolve.pl>
  4  */
  5 namespace Team3\PayU\Serializer;
  6 
  7 use JMS\Serializer\SerializationContext;
  8 use JMS\Serializer\SerializerInterface as JMSSerializerInterface;
  9 use Psr\Log\LoggerInterface;
 10 use Team3\PayU\Order\Model\OrderInterface;
 11 use Team3\PayU\Serializer\SerializerInterface as PayUSerializerInterface;
 12 
 13 /**
 14  * {@inheritdoc}
 15  *
 16  * Class Serializer
 17  * @package Team3\PayU\Serializer
 18  */
 19 class Serializer implements PayUSerializerInterface
 20 {
 21     /**
 22      * @var JMSSerializerInterface
 23      */
 24     protected $serializer;
 25 
 26     /**
 27      * @var GroupsSpecifierInterface
 28      */
 29     protected $groupsSpecifier;
 30 
 31     /**
 32      * @var LoggerInterface
 33      */
 34     protected $logger;
 35 
 36     /**
 37      * @param JMSSerializerInterface   $serializer
 38      * @param GroupsSpecifierInterface $groupsSpecifier
 39      * @param LoggerInterface          $logger
 40      */
 41     public function __construct(
 42         JMSSerializerInterface $serializer,
 43         GroupsSpecifierInterface $groupsSpecifier,
 44         LoggerInterface $logger
 45     ) {
 46         $this->serializer = $serializer;
 47         $this->groupsSpecifier = $groupsSpecifier;
 48         $this->logger = $logger;
 49     }
 50 
 51     /**
 52      * @param SerializableInterface $serializable
 53      * @param SerializationContext  $serializationContext
 54      *
 55      * @return string
 56      */
 57     public function toJson(
 58         SerializableInterface $serializable,
 59         SerializationContext $serializationContext = null
 60     ) {
 61         if (null == $serializationContext) {
 62             $serializationContext = new SerializationContext();
 63         }
 64 
 65         $serializationResult = $this
 66             ->serializer
 67             ->serialize(
 68                 $serializable,
 69                 'json',
 70                 $this->getSerializationContext($serializable, $serializationContext)
 71             );
 72         $this->logSerializationResult($serializable, $serializationResult);
 73 
 74         return $serializationResult;
 75     }
 76 
 77     /**
 78      * @param string $data
 79      * @param string $type
 80      *
 81      * @return array|object
 82      * @throws SerializerException
 83      */
 84     public function fromJson($data, $type)
 85     {
 86         try {
 87             $result = $this
 88                 ->serializer
 89                 ->deserialize(
 90                     $data,
 91                     $type,
 92                     'json'
 93                 );
 94         } catch (\Exception $exception) {
 95             $adaptedException = new SerializerException(
 96                 $exception->getMessage(),
 97                 $exception->getCode(),
 98                 $exception
 99             );
100             $this->logException($adaptedException);
101             throw $adaptedException;
102         }
103 
104         return $result;
105     }
106 
107     /**
108      * @param SerializableInterface $serializable
109      * @param SerializationContext  $serializationContext
110      *
111      * @return SerializationContext
112      */
113     private function getSerializationContext(
114         SerializableInterface $serializable,
115         SerializationContext $serializationContext
116     ) {
117         if ($serializable instanceof OrderInterface) {
118             $serializationContext->setGroups(
119                 $this->groupsSpecifier->specifyGroups($serializable)
120             );
121         }
122 
123         return $serializationContext;
124     }
125 
126     /**
127      * @param SerializableInterface $serializable
128      * @param string                $result
129      */
130     private function logSerializationResult(
131         SerializableInterface $serializable,
132         $result
133     ) {
134         $this
135             ->logger
136             ->debug(sprintf(
137                 'Serializable object %s was serialized to "%s"',
138                 get_class($serializable),
139                 $result
140             ));
141     }
142 
143     /**
144      * @param \Exception $exception
145      */
146     private function logException(\Exception $exception)
147     {
148         $this->logger->error(sprintf(
149             '%s exception occurred on deserialization with message "%s"',
150             get_class($exception),
151             $exception->getMessage()
152         ));
153     }
154 }
155 
PayU integration by Krzysztof Gzocha API documentation generated by ApiGen