Skip to content

Commit

Permalink
Update copyright year. PSR 1 / 2
Browse files Browse the repository at this point in the history
  • Loading branch information
niksamokhvalov committed May 27, 2016
1 parent 3eb942a commit b1442bc
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 280 deletions.
3 changes: 1 addition & 2 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright © 2015 Nik Samokhvalov (http://samokhvalov.info)
Copyright © 2015 — 2016 [Nik Samokhvalov](http://samokhvalov.info).

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,4 +19,3 @@ 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.

52 changes: 22 additions & 30 deletions src/Finder.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php
/**
* @link https://github.com/bitrix-expert/tools
* @copyright Copyright © 2015 Nik Samokhvalov
* @license MIT
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Bex\Tools;
Expand All @@ -12,10 +11,10 @@
use Bitrix\Main\Data\Cache;

/**
* Finder performs a fast search for data in standard structures of the Bitrix CMS with subsequent caching to speed
* Finder performs a fast search for data in standard structures of the Bitrix CMS with subsequent caching to speed
* up repeated queries.
*
* Finder uses the term "shard of the cache". The shards are used to separate the large volume cache for the logical
* Finder uses the term "shard of the cache". The shards are used to separate the large volume cache for the logical
* part. Each shard of the cache is stored separately.
*
* The basic Finder methods:
Expand All @@ -26,9 +25,9 @@
* Any instance Finder you can configure:
* * `setCacheTime()` — time life of the cache.
* * `setCacheDir()` — directory for cache.
*
*
* The logic reset of the cache by tag will be defined in a concrete implementation of Finder.
*
*
* @author Nik Samokhvalov <[email protected]>
*/
abstract class Finder
Expand All @@ -39,16 +38,16 @@ abstract class Finder

/**
* Constructor with filter parameters for Finder.
*
*
* Should not be called directly! Use *Tools classes (IblockTools, GroupTools, etc.)
*
*
* @param array $filter Filter paramters.
* @param bool $silenceMode When you use silence mode instead of an exception \Bex\Tools\ValueNotFoundException
* @param bool $silenceMode When you use silence mode instead of an exception \Bex\Tools\ValueNotFoundException
* (if value was be not found) is returned null.
*/
public function __construct(array $filter, $silenceMode = false)
{
$this->silenceMode = (bool) $silenceMode;
$this->silenceMode = (bool)$silenceMode;
}

/**
Expand Down Expand Up @@ -105,7 +104,7 @@ public static function setCacheDir($directory)

/**
* Returns value by filter from cache.
*
*
* @param array $cache All items for shard of the cache.
* @param array $filter Parameters for filter.
* @param string $shard Shard of the cache.
Expand All @@ -116,7 +115,7 @@ abstract protected function getValue(array $cache, array $filter, $shard);

/**
* Returns all items for shard of the cache.
*
*
* @param string $shard Shard of the cache.
*
* @return mixed
Expand All @@ -125,12 +124,12 @@ abstract protected function getItems($shard);

/**
* Returns items from cache. If cache expired will be executed request to DB (method $this->getItems()).
*
*
* @param array $filter Parameters for filter.
* @param string $shard Shard of the cache.
*
* @return mixed
*
*
* @throws ValueNotFoundException Value was not found.
* @throws \InvalidArgumentException Invalid type on filter.
*/
Expand All @@ -140,44 +139,37 @@ protected function getFromCache($filter = [], $shard = null)

$cache = Cache::createInstance();

if ($cache->initCache(static::getCacheTime(), null, static::getCacheDir() . '/' . $shard))
{
if ($cache->initCache(static::getCacheTime(), null, static::getCacheDir() . '/' . $shard)) {
$items = $cache->getVars();
}
else
{
} else {
$cache->startDataCache();
Application::getInstance()->getTaggedCache()->startTagCache(static::getCacheDir() . '/' . $shard);

$items = $this->getItems($shard);

if (!empty($items))
{
if (!empty($items)) {
Application::getInstance()->getTaggedCache()->endTagCache();

$cache->endDataCache($items);
}
else
{
} else {
$cache->abortDataCache();
}
}

try {
return $this->getValue($items, $filter, $shard);
} catch (ValueNotFoundException $e) {
if ($this->silenceMode)
{
if ($this->silenceMode) {
return null;
}

throw $e;
}
}

/**
* Registration of the tag cache.
*
*
* @param string $tag
*/
protected function registerCacheTag($tag)
Expand All @@ -187,7 +179,7 @@ protected function registerCacheTag($tag)

/**
* Deletes all cache by tag.
*
*
* @param string $tag
*/
protected static function deleteCacheByTag($tag)
Expand Down
77 changes: 29 additions & 48 deletions src/Group/GroupFinder.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php
/**
* @link https://github.com/bitrix-expert/tools
* @copyright Copyright © 2015 Nik Samokhvalov
* @license MIT
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Bex\Tools\Group;
Expand All @@ -15,7 +14,7 @@

/**
* Finder of the users groups.
*
*
* @author Nik Samokhvalov <[email protected]>
*/
class GroupFinder extends Finder
Expand All @@ -33,23 +32,19 @@ class GroupFinder extends Finder
public function __construct(array $filter, $silenceMode = false)
{
parent::__construct($filter, $silenceMode);

$filter = $this->prepareFilter($filter);

if (isset($filter['id']))
{
if (isset($filter['id'])) {
$this->id = $filter['id'];
}

if (isset($filter['code']))
{
if (isset($filter['code'])) {
$this->code = $filter['code'];
}

if (!isset($this->id))
{
if (!isset($this->code))
{
if (!isset($this->id)) {
if (!isset($this->code)) {
throw new ArgumentNullException('code');
}

Expand All @@ -61,7 +56,7 @@ public function __construct(array $filter, $silenceMode = false)

/**
* Gets group ID.
*
*
* @return integer
*/
public function id()
Expand All @@ -73,7 +68,7 @@ public function id()

/**
* Gets group code.
*
*
* @return string
*/
public function code()
Expand All @@ -85,28 +80,22 @@ public function code()

/**
* @inheritdoc
*
*
* @throws ArgumentNullException
*/
protected function prepareFilter(array $filter)
{
foreach ($filter as $code => &$value)
{
if ($code === 'id')
{
foreach ($filter as $code => &$value) {
if ($code === 'id') {
intval($value);

if ($value <= 0)
{
if ($value <= 0) {
throw new ArgumentNullException($code);
}
}
else
{
} else {
trim(htmlspecialchars($value));

if (strlen($value) <= 0)
{
if (strlen($value) <= 0) {
throw new ArgumentNullException($code);
}
}
Expand All @@ -120,27 +109,23 @@ protected function prepareFilter(array $filter)
*/
protected function getValue(array $cache, array $filter, $shard)
{
switch ($filter['type'])
{
switch ($filter['type']) {
case 'id':
if (isset($this->id))
{
if (isset($this->id)) {
return $this->id;
}

$value = (int) $cache['GROUPS_ID'][$this->code];
$value = (int)$cache['GROUPS_ID'][$this->code];

if ($value <= 0)
{
if ($value <= 0) {
throw new ValueNotFoundException('Group ID', 'group code "' . $this->code . '"');
}
break;

case 'code':
$value = $cache['GROUPS_CODE'][$this->id];

if (strlen($value) <= 0)
{
if (strlen($value) <= 0) {
throw new ValueNotFoundException('Group code', 'ID #' . $this->id);
}
break;
Expand All @@ -159,22 +144,19 @@ protected function getValue(array $cache, array $filter, $shard)
protected function getItems($shard)
{
$items = [];

$rsGroups = GroupTable::query()
->setSelect(['ID', 'STRING_ID'])
->exec();

while ($group = $rsGroups->fetch())
{
if ($group['STRING_ID'])
{
while ($group = $rsGroups->fetch()) {
if ($group['STRING_ID']) {
$items['GROUPS_ID'][$group['STRING_ID']] = $group['ID'];
$items['GROUPS_CODE'][$group['ID']] = $group['STRING_ID'];
}
}

if (!empty($items))
{
if (!empty($items)) {
Application::getInstance()->getTaggedCache()->registerTag(static::getCacheTag());
}

Expand All @@ -183,7 +165,7 @@ protected function getItems($shard)

/**
* Gets tag of cache.
*
*
* @return string
*/
public static function getCacheTag()
Expand All @@ -193,7 +175,7 @@ public static function getCacheTag()

/**
* Sets tag for cache.
*
*
* @param string $tag
*/
public static function setCacheTag($tag)
Expand Down Expand Up @@ -231,7 +213,7 @@ public static function onAfterGroupUpdate($id, &$fields)
{
static::deleteCacheByTag(static::getCacheTag());
}

/**
* @deprecated
* @see Finder::deleteCacheByTag()
Expand All @@ -240,8 +222,7 @@ protected static function deleteGroupCache()
{
global $CACHE_MANAGER;

if (defined('BX_COMP_MANAGED_CACHE'))
{
if (defined('BX_COMP_MANAGED_CACHE')) {
$CACHE_MANAGER->ClearByTag(static::getCacheTag());
}
}
Expand Down
Loading

0 comments on commit b1442bc

Please sign in to comment.