Skip to content

Commit

Permalink
add entity porperties, activate redir route with config checks
Browse files Browse the repository at this point in the history
  • Loading branch information
allphat committed Jan 15, 2022
1 parent 3be318b commit b92650e
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@
#backup files
*~

#PhpUnit
.phpunit.result.cache

# Backup entities generated with doctrine:generate:entities command
*/Entity/*~
14 changes: 9 additions & 5 deletions src/Controller/ShortyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,24 @@ public function __construct(ShortyManager $shortyManager)
* Route("/{code}", name="allphat_shorty_redirect", requirements={"code"= "^[\w]{6}$"})
* Method({"GET","HEAD"})
*/
/*public function redirectAction($code)
public function redirectAction(string $code)
{
$short = $this->get('shorty.manager')->findByCode($code);
$short = $this->get('shorty.manager')->findByCode($code, $this->getParameter('allphat_shorty.allow_secure_only'));

if (!$short) {
throw $this->createNotFoundException('Oopps this link does does not exist');
}

$allowMaxCalls = $this->getParameter('allphat_shorty.allow_max_calls');
if ($allowMaxCalls && $short->getCounter() >= $short->getMaxCalls()) {
throw $this->createNotFoundException('Oopps this link does does not exist anymore');
}

$this->get('shorty_manager')->updateCounter($short);

//@TODO create method to ckeck against options

return $this->redirect($short->getUrl(), 301);
}*/
return $this->redirect($this->getParameter('allphat_shorty.shorty_host') . '/' . $short->getCode(), 301);
}

/**
* @Route("/", name="allphat_shorty_create", service="shorty.controller")
Expand Down
21 changes: 16 additions & 5 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,26 @@ public function getConfigTreeBuilder()

$treeBuilder->getRootNode()
->children()
->booleanNode('allow_follow')
->scalarNode('shorty_host')
->info('The host defined for the sort url with scheme.')
->isRequired()
->cannotBeEmpty()
->end()
//->booleanNode('allow_follow')
// ->defaultValue(true)
//->end()
->booleanNode('allow_secure_only')
->info('Restrict to secure urls only.')
->defaultValue(false)
->end()
->booleanNode('allow_secure')
->booleaNode('allow_lifetime')
->info('Allow to define a lifetime for url validity.')
->defaultValue(false)
->end()
->integerNode('allow_lifetime')
->defaultValue(0)
->end()
->booleanNode('allow_max_calls')
->info('Allow to define a number of max usages for a shortened url.')
->defaultValue(false)
->end()
->end()
;

Expand Down
123 changes: 114 additions & 9 deletions src/Entity/ShortyEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,39 @@ class ShortyEntity

/**
*
* @ORM\Column(name="code", type="string", nullable = false, options={"aaaaaa"})
* @Assert\NotBlank()
* @ORM\Column(name="short_url", type="string", nullable = true)
* @Assert\Url()
*
* @var string
*/
private $shortUrl;

/**
*
* @ORM\Column(name="code", type="string", nullable = false)
* @Assert\NotBlank()
*
* @var string
*/
private $code;

/**
*
* @ORM\Column(name="source_url", type="string", nullable = false)
* @Assert\NotBlank()
* @Assert\Url()
*
* @var string
*/
private $sourceUrl;


/**
* @ORM\Column(name="is_used", type="boolean", options={"default":false})
*
* @var bool
*/
private $is_used;
private $isUsed;


/**
Expand All @@ -49,14 +67,36 @@ class ShortyEntity
* @var int
*/
private $createdAt;


/**
* @ORM\Column(name="starts_at", type="integer", nullable=false)
*
* @var int
*/
private $startsAt;

/**
* @ORM\Column(name="ends_at", type="integer", nullable=true)
*
* @var int|null
*/
private $endsAt;

/**
* Get code
* @ORM\Column(name="counter", type="integer", nullable=false, options={"default":0})
*
* @return string
* @var int
*/
public function getCode()
private $counter;

/**
* @ORM\Column(name="max_calls", type="integer", nullable=true)
*
* @var int
*/
private $maxCalls;

public function getCode(): string
{
return $this->code;
}
Expand All @@ -68,6 +108,25 @@ public function setCode(string $code): self
return $this;
}

public function setShortUrl(string $url): self
{
$this->shortUrl = $url;

return $this;
}

public function getSourceUrl(): string
{
return $this->sourceUrl;
}

public function setSourceUrl(string $url): self
{
$this->sourceUrl = $url;

return $this;
}

/**
* Get id
*
Expand All @@ -87,14 +146,14 @@ public function setId(int $id): self

public function setIsUsed(bool $isUsed): self
{
$this->is_used = $isUsed;
$this->isUsed = $isUsed;

return $this;
}

public function getIsUsed(): bool
{
return $this->is_used;
return $this->isUsed;
}

public function getCreatedAt(): int
Expand All @@ -108,4 +167,50 @@ public function setCreatedAt(int $createdAt): self

return $this;
}

public function setStartsAt(int $startsAt): self
{
$this->startsAt = $startsAt;

return $this;
}

public function getStartsAt()
{
return $this->startsAt;
}

public function setEndsAt(int $endsAt=null): self
{
$this->endsAt = $endsAt;
}

public function getEndsAt(): ?int
{
return $this->endsAt;
}

public function incrementCounter(): self
{
$this->counter++;

return $this;
}

public function getCounter(): int
{
return $this->counter;
}

public function setMaxCalls(int $max=null): self
{
$this->maxCalls = $max;

return $this;
}

public function getMaxCalls(): ?int
{
return $this->maxCalls;
}
}
20 changes: 14 additions & 6 deletions src/Manager/ShortyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public function __construct(ShortyRepository $shortyRepository)
$this->shortyRepository = $shortyRepository;
}

public function findByCode(string $code): ?ShortyEntity
public function findByCode(string $code, bool $allow_secure_only): ?ShortyEntity
{
return $this->shortyRepository->findOneBy(['code' => $code]);
return $this->shortyRepository->getByCode(['code' => $code, 'allow_secure_only' => $allow_secure_only]);
}

public function createEntity(): ShortyEntity
{
$short = new ShortyEntity();
$short->setCreatedAt((new \DateTime())->getTimestamp());
$short->setCode($this->encode());
$short->setCode($this->encodeString());
$short->setIsUsed(false);

$this->shortyRepository->persist($short);
Expand All @@ -38,8 +38,8 @@ public function createEntity(): ShortyEntity
public function createAndUseEntity(): ShortyEntity
{
$short = new ShortyEntity();
$short->setCreatedAt((new \DateTime())->getTimestamp());
$short->setCode($this->encode());
$short->setCreatedAt((new \DateTime())->getTimestamp());
$short->setCode($this->encodeString());
$short->setIsUsed(true);

return $short;
Expand All @@ -60,8 +60,16 @@ public function save(): ShortyEntity

return $short;
}

public function updateCounter(ShortyEntity $short): void
{
$short->incrementCounter();

$this->shortyRepository->persist($short);
$this->shortyRepository->save();
}

public function encode(): string
public function encodeString(): string
{
return Shortener::generateRandomCode();
}
Expand Down
15 changes: 14 additions & 1 deletion src/Repository/ShortyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,23 @@ public function save(): bool
return false;
}
}

public function getByCode(string $code, bool $alllowSecureOnly): ?ShortyEntity
{
$query = $this->createQueryBuilder('sg');
$query->select('sg');
$query->andWhere('sg.code = :code');
$query->setParameter('code', $code);
if (!$allowSecureOnly) {
$query->orWhere('sg.code = :insecure_code');
$query->setParameter('insecure_code', str_replace('https','http', $code));
}

return $query->getQuery()->getSingleResult();
}

public function findLast(): ?ShortyEntity
{
$entityManager = $this->getEntityManager();
$query = $this->createQueryBuilder('sg');
$query->select('sg');
$query->setMaxResults(1);
Expand Down
2 changes: 1 addition & 1 deletion tests/Manager/ShortyManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function setUp(): void

public function testEncode(): void
{
$this->assertMatchesRegularExpression('/\w{6}/', $this->manager->encode());
$this->assertMatchesRegularExpression('/\w{6}/', $this->manager->encodeString());
}

public function testCreateEntity(): void
Expand Down

0 comments on commit b92650e

Please sign in to comment.