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\PropertyExtractor;
  6 
  7 use Psr\Log\LoggerInterface;
  8 use ReflectionClass;
  9 use ReflectionException;
 10 use Team3\PayU\PropertyExtractor\Reader\ReaderInterface;
 11 
 12 /**
 13  * {@inheritdoc}
 14  *
 15  * Class Extractor
 16  * @package Team3\PayU\PropertyExtractor
 17  */
 18 class Extractor implements ExtractorInterface
 19 {
 20     /**
 21      * @var ReaderInterface
 22      */
 23     protected $reader;
 24 
 25     /**
 26      * @var LoggerInterface
 27      */
 28     protected $logger;
 29 
 30     /**
 31      * @param ReaderInterface $reader
 32      * @param LoggerInterface $logger
 33      */
 34     public function __construct(
 35         ReaderInterface $reader,
 36         LoggerInterface $logger
 37     ) {
 38         $this->reader = $reader;
 39         $this->logger = $logger;
 40     }
 41 
 42     /**
 43      * @param object $object
 44      *
 45      * @return ExtractorResult[]
 46      * @throws ExtractorException
 47      */
 48     public function extract($object)
 49     {
 50         $this->checkClass($object);
 51 
 52         try {
 53             return $this->processExtraction($object);
 54         } catch (ReflectionException $exception) {
 55             throw $this->adaptException($exception);
 56         }
 57     }
 58 
 59     /**
 60      * @param $object
 61      *
 62      * @return ExtractorResult[]
 63      */
 64     private function processExtraction($object)
 65     {
 66         $extracted = [];
 67         $reflectionClass = new ReflectionClass($object);
 68 
 69         foreach ($this->reader->read($object) as $readerResult) {
 70             $reflectionMethod = $reflectionClass->getMethod($readerResult->getMethodName());
 71             $reflectionMethod->setAccessible(true);
 72 
 73             $extractorResult = new ExtractorResult(
 74                 $readerResult->getPropertyName(),
 75                 $reflectionMethod->invoke($object)
 76             );
 77             $extracted[] = $extractorResult;
 78 
 79             $this->logExtractedResult($extractorResult, $object);
 80         }
 81 
 82         return $extracted;
 83     }
 84 
 85     /**
 86      * @param \Exception $exception
 87      *
 88      * @return ExtractorException
 89      */
 90     private function adaptException(\Exception $exception)
 91     {
 92         $this
 93             ->logger
 94             ->error(sprintf(
 95                 'Exception %s was throw during extracting properties. Message: "%s"',
 96                 get_class($exception),
 97                 $exception->getMessage()
 98             ));
 99 
100         return new ExtractorException(
101             $exception->getMessage(),
102             $exception->getCode(),
103             $exception
104         );
105     }
106 
107     /**
108      * @param $class
109      *
110      * @throws ExtractorException
111      */
112     private function checkClass($class)
113     {
114         if (!is_object($class)) {
115             $message = sprintf(
116                 'Given argument should be on object, but "%s" given',
117                 gettype($class)
118             );
119             $this
120                 ->logger
121                 ->error($message);
122             throw new ExtractorException($message);
123         }
124     }
125 
126     /**
127      * @param ExtractorResult $extractorResult
128      * @param object          $object
129      */
130     private function logExtractedResult(ExtractorResult $extractorResult, $object)
131     {
132         $this
133             ->logger
134             ->debug(sprintf(
135                 'Successfully extracted parameter %s with value "%s" from object %s',
136                 $extractorResult->getPropertyName(),
137                 print_r($extractorResult->getValue(), true),
138                 get_class($object)
139             ));
140     }
141 }
142 
PayU integration by Krzysztof Gzocha API documentation generated by ApiGen