Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
47 / 47 |
| Extractor | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
47 / 47 |
| __construct( ReaderInterface $reader, LoggerInterface $logger ) | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
| extract($object) | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
| processExtraction($object) | |
100.00% |
1 / 1 |
2 | |
100.00% |
13 / 13 |
|||
| adaptException(\Exception $exception) | |
100.00% |
1 / 1 |
1 | |
100.00% |
10 / 10 |
|||
| checkClass($class) | |
100.00% |
1 / 1 |
2 | |
100.00% |
9 / 9 |
|||
| logExtractedResult(ExtractorResult $extractorResult, $object) | |
100.00% |
1 / 1 |
1 | |
100.00% |
8 / 8 |
|||
| <?php | |
| /** | |
| * @author Krzysztof Gzocha <krzysztof.gzocha@xsolve.pl> | |
| */ | |
| namespace Team3\PayU\PropertyExtractor; | |
| use Psr\Log\LoggerInterface; | |
| use ReflectionClass; | |
| use ReflectionException; | |
| use Team3\PayU\PropertyExtractor\Reader\ReaderInterface; | |
| /** | |
| * {@inheritdoc} | |
| * | |
| * Class Extractor | |
| * @package Team3\PayU\PropertyExtractor | |
| */ | |
| class Extractor implements ExtractorInterface | |
| { | |
| /** | |
| * @var ReaderInterface | |
| */ | |
| protected $reader; | |
| /** | |
| * @var LoggerInterface | |
| */ | |
| protected $logger; | |
| /** | |
| * @param ReaderInterface $reader | |
| * @param LoggerInterface $logger | |
| */ | |
| public function __construct( | |
| ReaderInterface $reader, | |
| LoggerInterface $logger | |
| ) { | |
| $this->reader = $reader; | |
| $this->logger = $logger; | |
| } | |
| /** | |
| * @param object $object | |
| * | |
| * @return ExtractorResult[] | |
| * @throws ExtractorException | |
| */ | |
| public function extract($object) | |
| { | |
| $this->checkClass($object); | |
| try { | |
| return $this->processExtraction($object); | |
| } catch (ReflectionException $exception) { | |
| throw $this->adaptException($exception); | |
| } | |
| } | |
| /** | |
| * @param $object | |
| * | |
| * @return ExtractorResult[] | |
| */ | |
| private function processExtraction($object) | |
| { | |
| $extracted = []; | |
| $reflectionClass = new ReflectionClass($object); | |
| foreach ($this->reader->read($object) as $readerResult) { | |
| $reflectionMethod = $reflectionClass->getMethod($readerResult->getMethodName()); | |
| $reflectionMethod->setAccessible(true); | |
| $extractorResult = new ExtractorResult( | |
| $readerResult->getPropertyName(), | |
| $reflectionMethod->invoke($object) | |
| ); | |
| $extracted[] = $extractorResult; | |
| $this->logExtractedResult($extractorResult, $object); | |
| } | |
| return $extracted; | |
| } | |
| /** | |
| * @param \Exception $exception | |
| * | |
| * @return ExtractorException | |
| */ | |
| private function adaptException(\Exception $exception) | |
| { | |
| $this | |
| ->logger | |
| ->error(sprintf( | |
| 'Exception %s was throw during extracting properties. Message: "%s"', | |
| get_class($exception), | |
| $exception->getMessage() | |
| )); | |
| return new ExtractorException( | |
| $exception->getMessage(), | |
| $exception->getCode(), | |
| $exception | |
| ); | |
| } | |
| /** | |
| * @param $class | |
| * | |
| * @throws ExtractorException | |
| */ | |
| private function checkClass($class) | |
| { | |
| if (!is_object($class)) { | |
| $message = sprintf( | |
| 'Given argument should be on object, but "%s" given', | |
| gettype($class) | |
| ); | |
| $this | |
| ->logger | |
| ->error($message); | |
| throw new ExtractorException($message); | |
| } | |
| } | |
| /** | |
| * @param ExtractorResult $extractorResult | |
| * @param object $object | |
| */ | |
| private function logExtractedResult(ExtractorResult $extractorResult, $object) | |
| { | |
| $this | |
| ->logger | |
| ->debug(sprintf( | |
| 'Successfully extracted parameter %s with value "%s" from object %s', | |
| $extractorResult->getPropertyName(), | |
| print_r($extractorResult->getValue(), true), | |
| get_class($object) | |
| )); | |
| } | |
| } |