Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chippyash committed Jun 16, 2016
1 parent f44e05d commit 5c7df50
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 46 deletions.
13 changes: 13 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
exclude_paths:
- test/*
- docs/*
- vendor/*
ratings:
paths:
- "**.php"
engines:
phpmd:
enabled: true
phpcodesniffer:
enabled: true
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ php:
- 5.5
# aliased to a recent 5.6.x version
- 5.6
# aliased to a recent 7.x version
- 7
# hhvm
#- hhvm

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

## Quality Assurance

Certified for PHP 5.5+

![PHP 5.5](https://img.shields.io/badge/PHP-5.5-blue.svg)
![PHP 5.6](https://img.shields.io/badge/PHP-5.6-blue.svg)
![PHP 7](https://img.shields.io/badge/PHP-7-blue.svg)
[![Build Status](https://travis-ci.org/chippyash/Zend-Acl-Xml-Builder.svg?branch=master)](https://travis-ci.org/chippyash/Zend-Acl-Xml-Builder)
[![Test Coverage](https://codeclimate.com/github/chippyash/Zend-Acl-Xml-Builder/badges/coverage.svg)](https://codeclimate.com/github/chippyash/Zend-Acl-Xml-Builder/coverage)
[![Code Climate](https://codeclimate.com/github/chippyash/Zend-Acl-Xml-Builder/badges/gpa.svg)](https://codeclimate.com/github/chippyash/Zend-Acl-Xml-Builder)
Expand Down
11 changes: 7 additions & 4 deletions src/Chippyash/Zend/Acl/Xml/AbstractAclItemBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Zend\Permissions\Acl\Acl;
use Chippyash\BuilderPattern\AbstractBuilder;
use Zend\Permissions\Acl\AclInterface;

/**
* Build a zend-permissions-acl from an XML definition
Expand All @@ -23,15 +24,17 @@ abstract class AbstractAclItemBuilder extends AbstractBuilder
protected $dom;

/**
* @var Zend\Permissions\Acl\Acl
* @var Acl
*/
protected $acl;

/**
* Constructor
*
* @param \DOMDocument $xquery
* @param AclInterface $acl
* @param \DOMDocument $dom
* @param Acl|AclInterface $acl
*
* @internal param \DOMDocument $xquery
*/
public function __construct(\DOMDocument $dom, Acl $acl)
{
Expand All @@ -46,7 +49,7 @@ public function __construct(\DOMDocument $dom, Acl $acl)
protected function setBuildItems()
{
$this->buildItems = [
'result' => function() {
'result' => function () {
return $this->buildItem();
}
];
Expand Down
34 changes: 23 additions & 11 deletions src/Chippyash/Zend/Acl/Xml/AclBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

/**
* Build a zend-permissions-acl from an XML definition
*
* @property \DOMDocument xml
* @property RoleBuilder roles
* @property Acl acl
* @property ResourceBuilder resources
* @property RuleBuilder rules
*/
class AclBuilder extends AbstractBuilder
{
Expand Down Expand Up @@ -45,10 +51,10 @@ public function __construct(StringType $xmlFile, Acl $acl)
/**
* Check for imports in the document and process them.
* Converts imported xml into ACL and passes back to this builder.
*
*
* This is recursive so the end result is that we do a depth first leaf node
* construction of the ACL
*
*
* @param string $basePath Path to directory of parent file
*/
protected function processImports($basePath)
Expand Down Expand Up @@ -87,7 +93,7 @@ protected function processImports($basePath)
*
* @return \DOMDocument
*
* @throws AclBuilderBuilderException
* @throws AclBuilderException
*/
protected function loadDefinition($xmlFile, $xsdFile)
{
Expand All @@ -101,17 +107,21 @@ protected function loadDefinition($xmlFile, $xsdFile)
$defDom = new \DOMDocument();
$defDom->validateOnParse = true;
$options = LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0);
if ($isString) {
$defDom->loadXML($xmlFile, $options);
} else {
$defDom->load($xmlFile, $options);

switch ($isString) {
case true:
$defDom->loadXML($xmlFile, $options);
break;
default:
$defDom->load($xmlFile, $options);
}

$prevSetting = \libxml_use_internal_errors(true);
if (!$defDom->schemaValidate($xsdFile)) {
$errors = \libxml_get_errors();
$errMsg = '';
foreach ($errors as $error) {
$errMsg .= $this->libxml_display_error($error);
$errMsg .= $this->libxmlDisplayError($error);
}
throw new AclBuilderException(sprintf(self::ERR_INVALID_ACL, $errMsg));
}
Expand All @@ -136,10 +146,12 @@ protected function setBuildItems()

/**
* @link http://php.net/manual/en/domdocument.schemavalidate.php
* @param type $error
* @return type
*
* @param \LibXMLError $error
*
* @return string
*/
protected function libxml_display_error(\LibXMLError $error)
protected function libxmlDisplayError(\LibXMLError $error)
{
$return = "";
switch ($error->level) {
Expand Down
11 changes: 6 additions & 5 deletions src/Chippyash/Zend/Acl/Xml/AclDirector.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
<?php

/*
* Builder to build Acl from an XML file
*
* @copyright Ashley Kitson, UK, 2014
* @license GPL3.0+
*/

namespace Chippyash\Zend\Acl\Xml;

use Chippyash\BuilderPattern\AbstractDirector;
use Chippyash\Type\String\StringType;
use Chippyash\Zend\Acl\Xml\AclBuilder;
use Chippyash\Zend\Acl\Xml\AclRenderer;
use Zend\Permissions\Acl\Acl;

/**
Expand All @@ -23,6 +19,11 @@ class AclDirector extends AbstractDirector
public function __construct(StringType $xmlFile)
{
parent::__construct(
new AclBuilder($xmlFile, new Acl()), new AclRenderer());
new AclBuilder(
$xmlFile,
new Acl()
),
new AclRenderer()
);
}
}
4 changes: 3 additions & 1 deletion src/Chippyash/Zend/Acl/Xml/AclRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Chippyash\BuilderPattern\Renderer\PassthruRenderer;
use Chippyash\BuilderPattern\BuilderInterface;
use Zend\Permissions\Acl\Acl;

/**
* Returns the built ACL
Expand All @@ -21,7 +22,8 @@ class AclRenderer extends PassthruRenderer
* Render the built data
*
* @param BuilderInterface $builder
* @return Zend\Permissions\Acl\Acl
*
* @return Acl
*/
public function render(BuilderInterface $builder)
{
Expand Down
13 changes: 3 additions & 10 deletions src/Chippyash/Zend/Acl/Xml/ResourceBuilder.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<?php

/*
* Builder to build Acl from an XML file
*
* @copyright Ashley Kitson, UK, 2014
* @license GPL3.0+
*/

namespace Chippyash\Zend\Acl\Xml;

use Chippyash\Zend\Acl\Xml\AbstractAclItemBuilder;

/**
* Build zend-permissions-acl resources from an XML definition
*/
Expand All @@ -37,10 +33,6 @@ protected function addResources(\DOMNodeList $resources)
{
foreach ($resources as $resource) {
$parent = null;
if ($resource->hasAttribute('parent')) {
$parents = explode(',', $resource->getAttribute('parent'));
}

$resourceType = 'GenericResource';
if ($resource->hasAttribute('type')) {
$resourceType = $resource->getAttribute('type');
Expand All @@ -50,9 +42,10 @@ protected function addResources(\DOMNodeList $resources)

if ($resourceType !== 'GenericResource') {
$this->acl->addResource(new $resourceType($resourceName), $parent);
} else {
$this->acl->addResource($resourceName, $parent);
continue;
}

$this->acl->addResource($resourceName, $parent);
}
}
}
13 changes: 3 additions & 10 deletions src/Chippyash/Zend/Acl/Xml/RoleBuilder.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<?php

/*
* Builder to build Acl from an XML file
*
* @copyright Ashley Kitson, UK, 2014
* @license GPL3.0+
*/

namespace Chippyash\Zend\Acl\Xml;

use Chippyash\Zend\Acl\Xml\AbstractAclItemBuilder;

/**
* Build zend-permissions-acl roles from an XML definition
*/
Expand All @@ -30,7 +26,7 @@ public function buildItem()

/**
* Add roles to ACL
*
*
* @param \DOMNodeList $roles
*/
protected function addRoles(\DOMNodeList $roles)
Expand All @@ -48,11 +44,8 @@ protected function addRoles(\DOMNodeList $roles)

$roleName = $role->nodeValue;

if ($roleType !== 'GenericRole') {
$this->acl->addRole(new $roleType($roleName), $parents);
} else {
$this->acl->addRole($roleName, $parents);
}
$roleToAdd = ($roleType !== 'GenericRole' ? new $roleType($roleName) : $roleName);
$this->acl->addRole($roleToAdd, $parents);
}
}
}
3 changes: 0 additions & 3 deletions src/Chippyash/Zend/Acl/Xml/RuleBuilder.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php

/*
* Builder to build Acl from an XML file
*
* @copyright Ashley Kitson, UK, 2014
* @license GPL3.0+
*/

namespace Chippyash\Zend\Acl\Xml;

use Chippyash\Zend\Acl\Xml\AbstractAclItemBuilder;
use Zend\Permissions\Acl\Acl;

/**
Expand Down

0 comments on commit 5c7df50

Please sign in to comment.