Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
fist commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cengizhancaliskan committed Mar 28, 2017
0 parents commit 1dbc218
Show file tree
Hide file tree
Showing 14 changed files with 775 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CengizhanViewsCounterBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Cengizhan\ViewsCounterBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class CengizhanViewsCounterBundle extends Bundle
{
}
25 changes: 25 additions & 0 deletions DependencyInjection/CengizhanViewsCounterExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Cengizhan\ViewsCounterBundle\DependencyInjection;

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

class CengizhanViewsCounterExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('cengizhan_views_counter.use_query_builder', $config['use_query_builder']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
27 changes: 27 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Cengizhan\ViewsCounterBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('cengizhan_views_counter');

$rootNode
->addDefaultsIfNotSet()
->children()
->scalarNode('use_query_builder')->defaultFalse()->end()
->end()
;

return $treeBuilder;
}
}
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2017 Cengizhan Çalışkan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
85 changes: 85 additions & 0 deletions Manager/ViewsCounter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* ViewsCounter.
*
* (c) 2017 Cengizhan Çalışkan <cengizhancaliskan@gmail.com>
*
* This file is part of ViewsCount Bundle.
*/

namespace Cengizhan\ViewsCounterBundle\Manager;

use Cengizhan\ViewsCounterBundle\Model\ViewsCounterInterface;
use Cengizhan\ViewsCounterBundle\Model\VisitableInterface;
use Cengizhan\ViewsCounterBundle\Model\VisitableManagerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class ViewsCounter implements ViewsCounterInterface
{
const SESSION_KEY = '_views_counter';

/**
* @var SessionInterface
*/
private $session;

/**
* @var VisitableManagerInterface
*/
private $visitableManager;

/**
* @param SessionInterface $session
* @param VisitableManagerInterface $visitableManager
*/
public function __construct(SessionInterface $session, VisitableManagerInterface $visitableManager)
{
$this->session = $session;
$this->visitableManager = $visitableManager;
}

/**
* {@inheritdoc}
*/
public function count(VisitableInterface $visitable)
{
$viewsSession = $this->session->get(self::SESSION_KEY);

if (null === $viewsSession) { // unique view
$viewsSession = [];
$this->saveVisitable($viewsSession, $visitable);

$visitable->onUniqueViewed();
} elseif (!isset($viewsSession[$visitable->getVisitable()][$visitable->getVisitorId()])) { // unique view
$this->saveVisitable($viewsSession, $visitable);

$visitable->onUniqueViewed();
} elseif (isset($viewsSession[$visitable->getVisitable()][$visitable->getVisitorId()])) { // plural view
$visitable->onPluralViewed();
}

$this->visitableManager->update($visitable);
}

/**
* @param array $viewsSession
* @param VisitableInterface $visitable
*/
private function saveVisitable(array $viewsSession, $visitable)
{
$viewsSession[$visitable->getVisitable()] = [];

$this->saveVisitorId($viewsSession, $visitable);
}

/**
* @param array $viewsSession
* @param VisitableInterface $visitable
*/
private function saveVisitorId(array $viewsSession, $visitable)
{
$viewsSession[$visitable->getVisitable()][$visitable->getVisitorId()] = $visitable->getVisitorId();

$this->session->set(self::SESSION_KEY, $viewsSession);
}
}
58 changes: 58 additions & 0 deletions Manager/VisitableManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* VisitableManager.
*
* (c) 2017 Cengizhan Çalışkan <cengizhancaliskan@gmail.com>
*
* This file is part of ViewsCount Bundle.
*/

namespace Cengizhan\ViewsCounterBundle\Manager;

use Cengizhan\ViewsCounterBundle\Model\VisitableInterface;
use Cengizhan\ViewsCounterBundle\Model\VisitableManagerInterface;
use Doctrine\ORM\EntityManagerInterface;

class VisitableManager implements VisitableManagerInterface
{
private $em;

/**
* VisitableManager constructor.
*
* @param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}

/**
* {@inheritdoc}
*/
public function update(VisitableInterface $visitable)
{
$qb = $this->em->createQueryBuilder();

$qb->update(get_class($visitable), 'o')
->where('o.id = :id')
->setParameter('id', $visitable->getId())
;

if (true === $visitable->isSingularViewed()) {
$key = sprintf('o.%s', $visitable::SINGULAR_VIEW_FIELD);
$value = sprintf('%s + 1', $key);

$qb->set($key, $value);
}

if (true === $visitable->isPluralViewed()) {
$key = sprintf('o.%s', $visitable::PLURAL_VIEW_FIELD);
$value = sprintf('%s + 1', $key);

$qb->set($key, $value);
}

$qb->getQuery()->execute();
}
}
20 changes: 20 additions & 0 deletions Model/ViewsCounterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* ViewCounterInterface.
*
* (c) 2017 Cengizhan Çalışkan <cengizhancaliskan@gmail.com>
*
* This file is part of ViewsCount Bundle.
*/

namespace Cengizhan\ViewsCounterBundle\Model;

interface ViewsCounterInterface
{
/**
* Count singular and plural views and update the document/entity.
*
* @param VisitableInterface $visitable
*/
public function count(VisitableInterface $visitable);
}
84 changes: 84 additions & 0 deletions Model/VisitableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* VisitableInterface.
*
* (c) 2017 Cengizhan Çalışkan <cengizhancaliskan@gmail.com>
*
* This file is part of ViewsCount Bundle.
*/

namespace Cengizhan\ViewsCounterBundle\Model;

interface VisitableInterface
{
/**
* Singular Views entity field.
*
* @var string
*/
const SINGULAR_VIEW_FIELD = 'singularViewCount';

/**
* Plural Views entity field.
*
* @var string
*/
const PLURAL_VIEW_FIELD = 'pluralViewCount';

/**
* Session key.
*
* @var string
*/
const SESSION_KEY = '_views_count';

/**
* @return int
*/
public function getId();

/**
* @return bool
*/
public function isSingularViewed();

/**
* @return bool
*/
public function isPluralViewed();

/**
* Unique visitor id for every user.
*
* @return string
*/
public function getVisitorId();

/**
* Visitable name for every object/entity.
*
* @return string
*/
public function getVisitable();

/**
* Increase the number of singular views.
*
* @return int
*/
public function onSingularViewed();

/**
* Increase the number of plural views.
*
* @return int
*/
public function onPluralViewed();

/**
* Increase the number of unique views.
*
* @return $this
*/
public function onUniqueViewed();
}
20 changes: 20 additions & 0 deletions Model/VisitableManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* VisitableManagerInterface.
*
* (c) 2017 Cengizhan Çalışkan <cengizhancaliskan@gmail.com>
*
* This file is part of ViewsCount Bundle.
*/

namespace Cengizhan\ViewsCounterBundle\Model;

interface VisitableManagerInterface
{
/**
* Update views of the visitable object/entity.
*
* @param VisitableInterface $visitable
*/
public function update(VisitableInterface $visitable);
}
Loading

0 comments on commit 1dbc218

Please sign in to comment.