Skip to content

Commit

Permalink
Merge pull request #29 from DERHAEUPTLING/v2rework
Browse files Browse the repository at this point in the history
v2 rework
  • Loading branch information
m-vo authored Oct 27, 2020
2 parents 0a8f3af + e4db96b commit 3b2766f
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 227 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"require": {
"php": ">=7.2",
"ext-json": "*",
"ext-mbstring": "*",
"contao/core-bundle": "^4.8",
"guzzlehttp/guzzle": "^6.3",
"guzzlehttp/oauth-subscriber": "0.3.*",
Expand Down
59 changes: 40 additions & 19 deletions src/Annotation/Immoscout24ApiMapperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\Mapping\Column;

trait Immoscout24ApiMapperTrait
{
Expand Down Expand Up @@ -148,29 +149,49 @@ private static function autoMap($object, array $apiData): bool
}

// basic value
$object->$propertyName = $apiValue;
}
$enforceColumnTypes = static function (?Column $columnMapping, &$value): void {
if (null === $columnMapping) {
return;
}

return true;
}
if ($columnMapping->nullable && null === $value) {
return;
}

private static function getDateTime(string $value, \DateTime $fallback = null): \DateTime
{
try {
// extract and remove fractions as they won't be stored in the database (ISO 8601)
$dateTime = new \DateTime(
(new \DateTime($value))->format('c'),
new \DateTimeZone('GMT')
);
} catch (\Exception $e) {
// ignore
$dateTime = null;
}
$type = $columnMapping->type;

if ('integer' === $type) {
$value = (int) $value;

return;
}

if (!$dateTime instanceof \DateTime) {
$dateTime = $fallback ?? new \DateTime();
if ('float' === $type) {
$value = (float) $value;

return;
}

if ('boolean' === $type) {
$value = (bool) $value;

return;
}

if ('decimal' === $type) {
$value = number_format((float) $value, $columnMapping->scale, '.', '');
}

$value = (string) $value;
};

/** @var Column|null $columnMapping */
$columnMapping = $reader->getPropertyAnnotation($reflectionProperty, Column::class);
$enforceColumnTypes($columnMapping, $apiValue);

$object->$propertyName = $apiValue;
}

return $dateTime;
return true;
}
}
97 changes: 7 additions & 90 deletions src/Controller/FrontendModule/AbstractRealEstateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@
namespace Derhaeuptling\ContaoImmoscout24\Controller\FrontendModule;

use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
use Contao\CoreBundle\Translation\Translator;
use Contao\Template;
use Derhaeuptling\ContaoImmoscout24\Entity\RealEstate;
use Derhaeuptling\ContaoImmoscout24\Util\RealEstateFormatter;

abstract class AbstractRealEstateController extends AbstractFrontendModuleController
{
/** @var Translator */
private $translator;
/** @var RealEstateFormatter */
private $formatter;

/**
* RealEstateList constructor.
*/
public function __construct(Translator $translator)
public function __construct(RealEstateFormatter $formatter)
{
$this->translator = $translator;
$this->formatter = $formatter;
}

protected function addDataHelpers(Template $template): void
Expand Down Expand Up @@ -59,66 +56,7 @@ protected function getFormatted(RealEstate $realEstate, string $attribute): ?str
{
$rawValue = $realEstate->$attribute ?? null;

if (null === $rawValue) {
return null;
}

// don't alter strings and floats
if (\is_string($rawValue)) {
return $rawValue;
}
if (\is_float($rawValue)) {
return (string) $rawValue;
}

// convert booleans
if (\is_bool($rawValue)) {
return $rawValue ?
$this->translator->trans('immoscout24.yes', [], 'contao_default') :
$this->translator->trans('immoscout24.no', [], 'contao_default');
}

// try to resolve enumerations
if (\is_int($rawValue)) {
if ($rawValue >= 0) {
return $this->getEnumerationValue($attribute, $rawValue);
}

// resolve flags
$flags = [];
$value = -$rawValue;
$i = 0;

while (0 !== $value) {
if ($value & 1) {
$flags[] = 1 << $i;
}

++$i;
$value >>= 1;
}

// list as combined string
return implode(
' / ',
array_map(function ($value) use ($attribute) {
return $this->getEnumerationValue($attribute, $value);
}, $flags)
);
}

// flatten arrays to comma separated values
if (\is_array($rawValue)) {
return implode(', ', $rawValue);
}

// parse dates
if ($rawValue instanceof \DateTime) {
return $rawValue->format('d.m.Y H:i');
}

// fallback to 'none' value
return $this->translator->trans('immoscout24.none', [], 'contao_default');
return $this->formatter->format($rawValue, $attribute);
}

/**
Expand All @@ -128,27 +66,6 @@ protected function getFormatted(RealEstate $realEstate, string $attribute): ?str
*/
protected function getAllAttributesWithLabels(): array
{
// get all public fields as possible attributes
$attributes = array_keys(get_object_vars(new RealEstate()));

$attributesWithLabels = [];
foreach ($attributes as $attribute) {
$label = $this->translator->trans('immoscout24.'.$attribute, [], 'contao_default');
$attributesWithLabels[$attribute] = $label;
}

return $attributesWithLabels;
}

private function getEnumerationValue(string $attribute, int $rawValue): string
{
$key = sprintf('immoscout24.%s_.%d', $attribute, $rawValue);
$value = $this->translator->trans($key, [], 'contao_default');

if ($key !== $value) {
return $value;
}

return (string) $rawValue;
return $this->formatter->getAttributes();
}
}
6 changes: 3 additions & 3 deletions src/Controller/FrontendModule/RealEstateListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace Derhaeuptling\ContaoImmoscout24\Controller\FrontendModule;

use Contao\CoreBundle\Translation\Translator;
use Contao\ModuleModel;
use Contao\PageModel;
use Contao\StringUtil;
Expand All @@ -21,6 +20,7 @@
use Derhaeuptling\ContaoImmoscout24\Entity\RealEstate;
use Derhaeuptling\ContaoImmoscout24\Repository\AccountRepository;
use Derhaeuptling\ContaoImmoscout24\Repository\RealEstateRepository;
use Derhaeuptling\ContaoImmoscout24\Util\RealEstateFormatter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -35,9 +35,9 @@ class RealEstateListController extends AbstractRealEstateController
/**
* RealEstateList constructor.
*/
public function __construct(AccountRepository $accountRepository, RealEstateRepository $realEstateRepository, Translator $translator)
public function __construct(AccountRepository $accountRepository, RealEstateRepository $realEstateRepository, RealEstateFormatter $formatter)
{
parent::__construct($translator);
parent::__construct($formatter);

$this->accountRepository = $accountRepository;
$this->realEstateRepository = $realEstateRepository;
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/FrontendModule/RealEstateReaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
namespace Derhaeuptling\ContaoImmoscout24\Controller\FrontendModule;

use Contao\CoreBundle\Exception\PageNotFoundException;
use Contao\CoreBundle\Translation\Translator;
use Contao\Input;
use Contao\ModuleModel;
use Contao\StringUtil;
use Contao\Template;
use Derhaeuptling\ContaoImmoscout24\Entity\RealEstate;
use Derhaeuptling\ContaoImmoscout24\Repository\RealEstateRepository;
use Derhaeuptling\ContaoImmoscout24\Util\RealEstateFormatter;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -32,9 +32,9 @@ class RealEstateReaderController extends AbstractRealEstateController
/**
* RealEstateList constructor.
*/
public function __construct(Registry $doctrineRegistry, Translator $translator)
public function __construct(Registry $doctrineRegistry, RealEstateFormatter $formatter)
{
parent::__construct($translator);
parent::__construct($formatter);

$this->realEstateRepository = $doctrineRegistry->getRepository(RealEstate::class);
}
Expand Down
32 changes: 13 additions & 19 deletions src/Entity/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,6 @@ class Attachment extends DcaDefault
/** @var ContaoFramework|null */
private $framework;

/**
* @ORM\Column(name="created_at", type="datetime")
* [manually mapped]
*
* @var \DateTime
*/
private $createdAt;

/**
* @ORM\Column(name="modified_at", type="datetime")
* [manually mapped]
*
* @var \DateTime
*/
private $modifiedAt;

/**
* @ORM\Column(name="type", type="smallint")
* @Immoscout24Api(name="@xsi.type", enum={
Expand All @@ -84,6 +68,14 @@ class Attachment extends DcaDefault
*/
private $title = '';

/**
* @ORM\Column(name="modified_at", length=30)
* @Immoscout24Api(name="@modification")
*
* @var string
*/
private $modifiedAt;

/**
* @ORM\Column(name="is_floor_plan", type="boolean")
* @Immoscout24Api(name="floorplan", enum={
Expand Down Expand Up @@ -146,7 +138,6 @@ public function getTargetIdentifier(): string

public function update(self $newVersion): void
{
$this->createdAt = $newVersion->createdAt;
$this->modifiedAt = $newVersion->modifiedAt;
$this->title = $newVersion->title;
$this->isFloorPlan = $newVersion->isFloorPlan;
Expand Down Expand Up @@ -187,6 +178,11 @@ public function getTitle(): string
return $this->title;
}

public function getModifiedAt(): string
{
return $this->modifiedAt;
}

public function getFile(): ?FilesModel
{
if (self::CONTENT_READY !== $this->getState()) {
Expand Down Expand Up @@ -319,8 +315,6 @@ public static function createFromApiResponse(array $data, RealEstate $realEstate
$attachment->scraperUrls = serialize($urlData);

$attachment->realEstate = $realEstate;
$attachment->createdAt = self::getDateTime($data['creationDate'] ?? '');
$attachment->modifiedAt = self::getDateTime($data['lastModificationDate'] ?? '', $attachment->createdAt);

// automatically mapped values
if (self::autoMap($attachment, $data)) {
Expand Down
Loading

0 comments on commit 3b2766f

Please sign in to comment.