Normalized entities adding missing type hints and removing superfluous comments

This commit is contained in:
Alejandro Celaya 2018-09-15 10:03:42 +02:00
parent 4f2146dd9c
commit 07165f344f
9 changed files with 78 additions and 247 deletions

View File

@ -8,26 +8,19 @@ use Doctrine\ORM\Mapping as ORM;
abstract class AbstractEntity
{
/**
* @var int
* @var string
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\Column(name="id", type="bigint", options={"unsigned"=true})
*/
protected $id;
/**
* @return int
*/
public function getId()
public function getId(): string
{
return $this->id;
}
/**
* @param int $id
* @return $this
*/
public function setId($id)
public function setId(string $id): self
{
$this->id = $id;
return $this;

View File

@ -7,13 +7,14 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
/**
* Class ShortUrl
* @author
* @link
*
* @ORM\Entity(repositoryClass="Shlinkio\Shlink\Core\Repository\ShortUrlRepository")
* @ORM\Entity(repositoryClass=ShortUrlRepository::class)
* @ORM\Table(name="short_urls")
*/
class ShortUrl extends AbstractEntity
@ -22,7 +23,7 @@ class ShortUrl extends AbstractEntity
* @var string
* @ORM\Column(name="original_url", type="string", nullable=false, length=1024)
*/
protected $originalUrl;
private $originalUrl;
/**
* @var string
* @ORM\Column(
@ -33,17 +34,17 @@ class ShortUrl extends AbstractEntity
* unique=true
* )
*/
protected $shortCode;
private $shortCode;
/**
* @var \DateTime
* @ORM\Column(name="date_created", type="datetime")
*/
protected $dateCreated;
private $dateCreated;
/**
* @var Collection|Visit[]
* @ORM\OneToMany(targetEntity=Visit::class, mappedBy="shortUrl", fetch="EXTRA_LAZY")
*/
protected $visits;
private $visits;
/**
* @var Collection|Tag[]
* @ORM\ManyToMany(targetEntity=Tag::class, cascade={"persist"})
@ -53,46 +54,36 @@ class ShortUrl extends AbstractEntity
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
* })
*/
protected $tags;
private $tags;
/**
* @var \DateTime
* @ORM\Column(name="valid_since", type="datetime", nullable=true)
*/
protected $validSince;
private $validSince;
/**
* @var \DateTime
* @ORM\Column(name="valid_until", type="datetime", nullable=true)
*/
protected $validUntil;
private $validUntil;
/**
* @var integer
* @ORM\Column(name="max_visits", type="integer", nullable=true)
*/
protected $maxVisits;
private $maxVisits;
/**
* ShortUrl constructor.
*/
public function __construct()
{
$this->shortCode = '';
$this->dateCreated = new \DateTime();
$this->visits = new ArrayCollection();
$this->shortCode = '';
$this->tags = new ArrayCollection();
}
/**
* @return string
*/
public function getLongUrl(): string
{
return $this->originalUrl;
}
/**
* @param string $longUrl
* @return $this
*/
public function setLongUrl(string $longUrl): self
{
$this->originalUrl = $longUrl;
@ -100,7 +91,6 @@ class ShortUrl extends AbstractEntity
}
/**
* @return string
* @deprecated Use getLongUrl() instead
*/
public function getOriginalUrl(): string
@ -109,8 +99,6 @@ class ShortUrl extends AbstractEntity
}
/**
* @param string $originalUrl
* @return $this
* @deprecated Use setLongUrl() instead
*/
public function setOriginalUrl(string $originalUrl): self
@ -118,37 +106,23 @@ class ShortUrl extends AbstractEntity
return $this->setLongUrl($originalUrl);
}
/**
* @return string
*/
public function getShortCode(): string
{
return $this->shortCode;
}
/**
* @param string $shortCode
* @return $this
*/
public function setShortCode(string $shortCode)
public function setShortCode(string $shortCode): self
{
$this->shortCode = $shortCode;
return $this;
}
/**
* @return \DateTime
*/
public function getDateCreated(): \DateTime
{
return $this->dateCreated;
}
/**
* @param \DateTime $dateCreated
* @return $this
*/
public function setDateCreated(\DateTime $dateCreated)
public function setDateCreated(\DateTime $dateCreated): self
{
$this->dateCreated = $dateCreated;
return $this;
@ -164,55 +138,36 @@ class ShortUrl extends AbstractEntity
/**
* @param Collection|Tag[] $tags
* @return $this
*/
public function setTags(Collection $tags)
public function setTags(Collection $tags): self
{
$this->tags = $tags;
return $this;
}
/**
* @param Tag $tag
* @return $this
*/
public function addTag(Tag $tag)
public function addTag(Tag $tag): self
{
$this->tags->add($tag);
return $this;
}
/**
* @return \DateTime|null
*/
public function getValidSince()
public function getValidSince(): ?\DateTime
{
return $this->validSince;
}
/**
* @param \DateTime|null $validSince
* @return $this|self
*/
public function setValidSince($validSince): self
public function setValidSince(?\DateTime $validSince): self
{
$this->validSince = $validSince;
return $this;
}
/**
* @return \DateTime|null
*/
public function getValidUntil()
public function getValidUntil(): ?\DateTime
{
return $this->validUntil;
}
/**
* @param \DateTime|null $validUntil
* @return $this|self
*/
public function setValidUntil($validUntil): self
public function setValidUntil(?\DateTime $validUntil): self
{
$this->validUntil = $validUntil;
return $this;
@ -224,7 +179,7 @@ class ShortUrl extends AbstractEntity
}
/**
* @param Collection $visits
* @param Collection|Visit[] $visits
* @return ShortUrl
* @internal
*/
@ -234,19 +189,12 @@ class ShortUrl extends AbstractEntity
return $this;
}
/**
* @return int|null
*/
public function getMaxVisits()
public function getMaxVisits(): ?int
{
return $this->maxVisits;
}
/**
* @param int|null $maxVisits
* @return $this|self
*/
public function setMaxVisits($maxVisits): self
public function setMaxVisits(?int $maxVisits): self
{
$this->maxVisits = $maxVisits;
return $this;

View File

@ -21,39 +21,25 @@ class Tag extends AbstractEntity implements \JsonSerializable
* @var string
* @ORM\Column(unique=true)
*/
protected $name;
private $name;
public function __construct($name = null)
{
$this->name = $name;
}
/**
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName($name)
public function setName(string $name)
{
$this->name = $name;
return $this;
}
/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
public function jsonSerialize(): string
{
return $this->name;
}

View File

@ -7,13 +7,14 @@ use Doctrine\ORM\Mapping as ORM;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Common\Exception\WrongIpException;
use Shlinkio\Shlink\Common\Util\IpAddress;
use Shlinkio\Shlink\Core\Repository\VisitRepository;
/**
* Class Visit
* @author
* @link
*
* @ORM\Entity(repositoryClass="Shlinkio\Shlink\Core\Repository\VisitRepository")
* @ORM\Entity(repositoryClass=VisitRepository::class)
* @ORM\Table(name="visits")
*/
class Visit extends AbstractEntity implements \JsonSerializable

View File

@ -21,159 +21,110 @@ class VisitLocation extends AbstractEntity implements ArraySerializableInterface
* @var string
* @ORM\Column(nullable=true)
*/
protected $countryCode;
private $countryCode;
/**
* @var string
* @ORM\Column(nullable=true)
*/
protected $countryName;
private $countryName;
/**
* @var string
* @ORM\Column(nullable=true)
*/
protected $regionName;
private $regionName;
/**
* @var string
* @ORM\Column(nullable=true)
*/
protected $cityName;
private $cityName;
/**
* @var string
* @ORM\Column(nullable=true)
*/
protected $latitude;
private $latitude;
/**
* @var string
* @ORM\Column(nullable=true)
*/
protected $longitude;
private $longitude;
/**
* @var string
* @ORM\Column(nullable=true)
*/
protected $timezone;
private $timezone;
/**
* @return string
*/
public function getCountryCode()
public function getCountryCode(): string
{
return $this->countryCode;
return $this->countryCode ?? '';
}
/**
* @param string $countryCode
* @return $this
*/
public function setCountryCode($countryCode)
public function setCountryCode(string $countryCode)
{
$this->countryCode = $countryCode;
return $this;
}
/**
* @return string
*/
public function getCountryName()
public function getCountryName(): string
{
return $this->countryName;
return $this->countryName ?? '';
}
/**
* @param string $countryName
* @return $this
*/
public function setCountryName($countryName)
public function setCountryName(string $countryName): self
{
$this->countryName = $countryName;
return $this;
}
/**
* @return string
*/
public function getRegionName()
public function getRegionName(): string
{
return $this->regionName;
return $this->regionName ?? '';
}
/**
* @param string $regionName
* @return $this
*/
public function setRegionName($regionName)
public function setRegionName(string $regionName): self
{
$this->regionName = $regionName;
return $this;
}
/**
* @return string
*/
public function getCityName()
public function getCityName(): string
{
return $this->cityName;
return $this->cityName ?? '';
}
/**
* @param string $cityName
* @return $this
*/
public function setCityName($cityName)
public function setCityName(string $cityName): self
{
$this->cityName = $cityName;
return $this;
}
/**
* @return string
*/
public function getLatitude()
public function getLatitude(): string
{
return $this->latitude;
return $this->latitude ?? '';
}
/**
* @param string $latitude
* @return $this
*/
public function setLatitude($latitude)
public function setLatitude(string $latitude): self
{
$this->latitude = $latitude;
return $this;
}
/**
* @return string
*/
public function getLongitude()
public function getLongitude(): string
{
return $this->longitude;
return $this->longitude ?? '';
}
/**
* @param string $longitude
* @return $this
*/
public function setLongitude($longitude)
public function setLongitude(string $longitude): self
{
$this->longitude = $longitude;
return $this;
}
/**
* @return string
*/
public function getTimezone()
public function getTimezone(): string
{
return $this->timezone;
return $this->timezone ?? '';
}
/**
* @param string $timezone
* @return $this
*/
public function setTimezone($timezone)
public function setTimezone(string $timezone): self
{
$this->timezone = $timezone;
return $this;
@ -181,41 +132,36 @@ class VisitLocation extends AbstractEntity implements ArraySerializableInterface
/**
* Exchange internal values from provided array
*
* @param array $array
* @return void
*/
public function exchangeArray(array $array)
public function exchangeArray(array $array): void
{
if (array_key_exists('country_code', $array)) {
if (\array_key_exists('country_code', $array)) {
$this->setCountryCode($array['country_code']);
}
if (array_key_exists('country_name', $array)) {
if (\array_key_exists('country_name', $array)) {
$this->setCountryName($array['country_name']);
}
if (array_key_exists('region_name', $array)) {
if (\array_key_exists('region_name', $array)) {
$this->setRegionName($array['region_name']);
}
if (array_key_exists('city', $array)) {
if (\array_key_exists('city', $array)) {
$this->setCityName($array['city']);
}
if (array_key_exists('latitude', $array)) {
if (\array_key_exists('latitude', $array)) {
$this->setLatitude($array['latitude']);
}
if (array_key_exists('longitude', $array)) {
if (\array_key_exists('longitude', $array)) {
$this->setLongitude($array['longitude']);
}
if (array_key_exists('time_zone', $array)) {
if (\array_key_exists('time_zone', $array)) {
$this->setTimezone($array['time_zone']);
}
}
/**
* Return an array representation of the object
*
* @return array
*/
public function getArrayCopy()
public function getArrayCopy(): array
{
return [
'countryCode' => $this->countryCode,
@ -228,14 +174,7 @@ class VisitLocation extends AbstractEntity implements ArraySerializableInterface
];
}
/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->getArrayCopy();
}

View File

@ -54,7 +54,7 @@ class UrlShortenerTest extends TestCase
$this->em->persist(Argument::any())->will(function ($arguments) {
/** @var ShortUrl $shortUrl */
$shortUrl = $arguments[0];
$shortUrl->setId(10);
$shortUrl->setId('10');
});
$repo = $this->prophesize(ObjectRepository::class);
$repo->findOneBy(Argument::any())->willReturn(null);

View File

@ -23,17 +23,17 @@ class ApiKey extends AbstractEntity
* @var string
* @ORM\Column(name="`key`", nullable=false, unique=true)
*/
protected $key;
private $key;
/**
* @var \DateTime|null
* @ORM\Column(name="expiration_date", nullable=true, type="datetime")
*/
protected $expirationDate;
private $expirationDate;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
protected $enabled;
private $enabled;
public function __construct()
{
@ -41,45 +41,28 @@ class ApiKey extends AbstractEntity
$this->key = $this->generateV4Uuid();
}
/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}
/**
* @param string $key
* @return $this
*/
public function setKey($key): self
public function setKey(string $key): self
{
$this->key = $key;
return $this;
}
/**
* @return \DateTime|null
*/
public function getExpirationDate(): ?\DateTime
{
return $this->expirationDate;
}
/**
* @param \DateTime $expirationDate
* @return $this
*/
public function setExpirationDate($expirationDate): self
public function setExpirationDate(\DateTime $expirationDate): self
{
$this->expirationDate = $expirationDate;
return $this;
}
/**
* @return bool
*/
public function isExpired(): bool
{
if ($this->expirationDate === null) {
@ -89,29 +72,17 @@ class ApiKey extends AbstractEntity
return $this->expirationDate < new \DateTime();
}
/**
* @return boolean
*/
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* @param boolean $enabled
* @return $this
*/
public function setEnabled($enabled): self
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
/**
* Disables this API key
*
* @return $this
*/
public function disable(): self
{
return $this->setEnabled(false);
@ -119,19 +90,12 @@ class ApiKey extends AbstractEntity
/**
* Tells if this api key is enabled and not expired
*
* @return bool
*/
public function isValid(): bool
{
return $this->isEnabled() && ! $this->isExpired();
}
/**
* The string representation of an API key is the key itself
*
* @return string
*/
public function __toString(): string
{
return $this->getKey();

View File

@ -55,7 +55,7 @@ class AuthenticateActionTest extends TestCase
*/
public function properApiKeyReturnsTokenInResponse()
{
$this->apiKeyService->getByKey('foo')->willReturn((new ApiKey())->setId(5))
$this->apiKeyService->getByKey('foo')->willReturn((new ApiKey())->setId('5'))
->shouldBeCalledTimes(1);
$request = ServerRequestFactory::fromGlobals()->withParsedBody([

View File

@ -30,7 +30,7 @@ class JWTServiceTest extends TestCase
*/
public function tokenIsProperlyCreated()
{
$id = 34;
$id = '34';
$token = $this->service->create((new ApiKey())->setId($id));
$payload = (array) JWT::decode($token, 'foo', [JWTService::DEFAULT_ENCRYPTION_ALG]);
$this->assertGreaterThanOrEqual($payload['iat'], time());