Skip to content

Commit

Permalink
Merge pull request #2349 from forkcms/location-entity
Browse files Browse the repository at this point in the history
Location(Setting) entity
  • Loading branch information
jeroendesloovere committed Apr 24, 2018
2 parents 7e3490b + 46161a9 commit ba444b4
Show file tree
Hide file tree
Showing 15 changed files with 2,867 additions and 2,260 deletions.
2 changes: 1 addition & 1 deletion src/Backend/Modules/Faq/Tests/Engine/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private function getUpdateCategoryData()
private function getUpdatedCategoryMetaData()
{
return [
'id' => 28,
'id' => 29,
'keywords' => 'Test edit category',
'description' => 'Test edit category',
'title' => 'Test edit category',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

Expand All @@ -12,11 +13,17 @@
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class LocationExtension extends Extension
class LocationExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}

public function prepend(ContainerBuilder $container): void
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('doctrine.yml');
}
}
331 changes: 331 additions & 0 deletions src/Backend/Modules/Location/Domain/Location/Location.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
<?php

namespace Backend\Modules\Location\Domain\Location;

use Backend\Modules\Location\Domain\LocationSetting\LocationSetting;
use Common\Exception\CanNotSetExtraIdException;
use Common\Locale;
use Backend\Core\Language\Locale as BackendLocale;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Frontend\Core\Language\Locale as FrontendLocale;

/**
* @ORM\Table(name="location")
* @ORM\Entity(repositoryClass="Backend\Modules\Location\Domain\Location\LocationRepository")
* @ORM\HasLifecycleCallbacks
*/
class Location
{
/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @var Locale
*
* @ORM\Column(type="locale")
*/
private $locale;

/**
* @TODO: map this to some sort of Extra entity
* @var integer
*
* @ORM\Column(type="integer")
*/
private $extraId;

/**
* @var string
*
* @ORM\Column(type="string")
*/
private $title;

/**
* @var string
*
* @ORM\Column(type="string")
*/
private $street;

/**
* @var string
*
* @ORM\Column(type="string")
*/
private $number;

/**
* @var string
*
* @ORM\Column(type="string")
*/
private $zip;

/**
* @var string
*
* @ORM\Column(type="string")
*/
private $city;

/**
* @var string
*
* @ORM\Column(type="string")
*/
private $country;

/**
* @var float
*
* @ORM\Column(type="float")
*/
private $latitude;

/**
* @var float
*
* @ORM\Column(type="float")
*/
private $longitude;

/**
* @var bool
*
* @ORM\Column(type="boolean", options={"default" = true})
*/
private $showInOverview;

/**
* @var Collection
*
* @ORM\OneToMany(
* targetEntity="Backend\Modules\Location\Domain\LocationSetting\LocationSetting",
* mappedBy="location",
* cascade={"persist"},
* orphanRemoval=true
* )
*/
private $settings;

/**
* @var DateTime
*
* @ORM\Column(type="datetime")
*/
private $createdOn;

/**
* @var DateTime
*
* @ORM\Column(type="datetime")
*/
private $editedOn;

public function __construct(
Locale $locale,
string $title,
string $street,
string $number,
string $zip,
string $city,
string $country,
float $latitude,
float $longitude,
bool $showInOverview = true,
int $extraId = null
) {
$this->locale = $locale;
$this->title = $title;
$this->street = $street;
$this->number = $number;
$this->zip = $zip;
$this->city = $city;
$this->country = $country;
$this->latitude = $latitude;
$this->longitude = $longitude;
$this->showInOverview = $showInOverview;
$this->extraId = $extraId;

$this->settings = new ArrayCollection();
}

public function update(
string $title,
string $street,
string $number,
string $zip,
string $city,
string $country,
float $latitude,
float $longitude,
bool $showInOverview = true
) {
$this->title = $title;
$this->street = $street;
$this->number = $number;
$this->zip = $zip;
$this->city = $city;
$this->country = $country;
$this->latitude = $latitude;
$this->longitude = $longitude;
$this->showInOverview = $showInOverview;
}

public function getId(): int
{
return $this->id;
}

public function getLocale(): Locale
{
return $this->locale;
}

public function getExtraId(): int
{
return $this->extraId;
}

public function getTitle(): string
{
return $this->title;
}

public function getStreet(): string
{
return $this->street;
}

public function getNumber(): string
{
return $this->number;
}

public function getZip(): string
{
return $this->zip;
}

public function getCity(): string
{
return $this->city;
}

public function getCountry(): string
{
return $this->country;
}

public function getLatitude(): float
{
return $this->latitude;
}

public function getLongitude(): float
{
return $this->longitude;
}

public function isShowInOverview(): bool
{
return $this->showInOverview;
}

public function getSettings(): Collection
{
return $this->settings;
}

public function getCreatedOn(): DateTime
{
return $this->createdOn;
}

public function getEditedOn(): DateTime
{
return $this->editedOn;
}

public function setExtraId(int $extraId): void
{
if ($this->extraId !== null) {
throw new CanNotSetExtraIdException();
}

$this->extraId = $extraId;
}

public function addSetting(LocationSetting $setting): void
{
$this->settings->add($setting);
}

/**
* @ORM\PrePersist
*/
public function prePersist(): void
{
$this->createdOn = $this->editedOn = new DateTime();
}

/**
* @ORM\PreUpdate
*/
public function preUpdate(): void
{
$this->editedOn = new DateTime();
}

public static function fromArray(array $item): self
{
if (APPLICATION === 'Frontend') {
$locale = FrontendLocale::fromString($item['language']);
} else {
$locale = BackendLocale::fromString($item['language']);
}

return new self(
$locale,
$item['title'],
$item['street'],
$item['number'],
$item['zip'],
$item['city'],
$item['country'],
$item['lat'],
$item['lng'],
$item['show_overview'] ?? true,
$item['extra_id'] ?? null
);
}

public function toArray(): array
{
return [
'id' => $this->id,
'language' => $this->locale->getLocale(),
'title' => $this->title,
'street' => $this->street,
'number' => $this->number,
'zip' => $this->zip,
'city' => $this->city,
'country' => $this->country,
'lat' => $this->latitude,
'lng' => $this->longitude,
'show_overview' => (int) $this->showInOverview,
'extra_id' => $this->extraId,
'settings' => $this->settings->toArray(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Backend\Modules\Location\Domain\Location;

use Doctrine\ORM\EntityRepository;

final class LocationRepository extends EntityRepository
{
public function add(Location $location): void
{
$this->getEntityManager()->persist($location);
$this->getEntityManager()->flush();
}

public function save(Location $location): void
{
$this->getEntityManager()->flush($location);
}

public function remove(Location $location): void
{
$this->getEntityManager()->remove($location);
$this->getEntityManager()->flush();
}
}
Loading

0 comments on commit ba444b4

Please sign in to comment.