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

Move service logic to kernel #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "dev-master",
"doctrine/common": "dev-master",
"ensemble/utils": "dev-master"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
/**
* Class for page service
*/
'page_service_class' => '',
'page_service_class' => 'Ensemble\Kernel\Service\Page',

/**
* Whether page parsing (routes + navigation) is enabled
Expand Down
25 changes: 16 additions & 9 deletions config/services.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,41 @@
* @link http://ensemble.github.com
*/

use Ensemble\Kernel\Exception;
use Ensemble\Kernel\Listener;
use Ensemble\Kernel\Parser;
use Ensemble\Kernel\Service;

use Ensemble\Kernel\Exception;

return array(
'factories' => array(
'Ensemble\Kernel\Service\Page' => function ($sm) {
$config = $sm->get('config');
$config = $sm->get('Config');
$config = $config['ensemble_kernel'];
if (empty($config['page_service_class'])) {
throw new Exception\PageServiceNotFoundException(
'No service manager key provided for an service adapter'
'No service manager key provided for a service adapter'
);
}

$service = $sm->get($config['page_service_class']);
$pageMapper = $sm->get('Ensemble\Kernel\Mapper\Page');
$serviceClass = $config['page_service_class'];
$service = new $serviceClass($pageMapper);

if (!$service instanceof Service\PageInterface) {
if (!$service instanceof Service\Page) {
throw new Exception\PageServiceNotFoundException(
'Instance of service adapter does not implement Ensemble\Kernel\Service\PageInterface'
'Service is not an instance or subclass of Ensemble\Kernel\Service\Page'
);
}
return $service;
},
'Ensemble\Kernel\Mapper\Page' => function () {
throw new Exception\PageMapperNotFoundException(
'No mapper implementing Ensemble\Kernel\Mapper\PageInterface was found. Please use
Ensemble\KernelDoctrineOrm or implement your own mapper under this key'
);
},
'Ensemble\Kernel\Listener\DefaultListenerAggregate' => function ($sm) {
$config = $sm->get('configuration');
$config = $sm->get('Config');
$config = $config['ensemble_kernel'];

$listener = new Listener\DefaultListenerAggregate;
Expand Down Expand Up @@ -148,7 +155,7 @@
return $listener;
},
'Ensemble\Kernel\Parser\Route' => function ($sm) {
$config = $sm->get('config');
$config = $sm->get('Config');
$routes = $config['cmf_routes'];

$parser = new Parser\Route;
Expand Down
46 changes: 46 additions & 0 deletions src/Kernel/Exception/PageMapperNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright (c) 2012 Soflomo http://soflomo.com.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the names of the copyright holders nor the names of the
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Ensemble\Kernel
* @author Jurian Sluiman <jurian@soflomo.com>
* @copyright 2012 Soflomo http://soflomo.com.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://ensemble.github.com
*/

namespace Ensemble\Kernel\Exception;

class PageMapperNotFoundException extends \RuntimeException
{
}
81 changes: 81 additions & 0 deletions src/Kernel/Mapper/PageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Copyright (c) 2012 Soflomo http://soflomo.com.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the names of the copyright holders nor the names of the
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Ensemble\KernelDoctrineORM
* @author Jurian Sluiman <jurian@soflomo.com>
* @copyright 2012 Soflomo http://soflomo.com.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://ensemble.github.com
*/

namespace Ensemble\Kernel\Mapper;

use Doctrine\Common\Persistence\ObjectRepository;
use Ensemble\Kernel\Model\PageInterface as PageModel;

interface PageInterface extends ObjectRepository
{
/**
* Persist a new page in the persistency layer
*
* @param PageModel $page Page to persist
* @return PageModel The persisted page
*/
public function persist(PageModel $page);

/**
* Update an existing page
*
* @param PageModel $page Page to update
* @return PageModel The updated page
*/
public function update(PageModel $page);

/**
* Delete a page
*
* @param PageModel $page Page to delete
* @return bool True if page is deleted
*/
public function delete(PageModel $page);

/**
* Get all root nodes
*
* @param string|null $sortByField
* @param string $direction
* @return mixed
*/
public function getRootNodes($sortByField = null, $direction = 'ASC');
}
125 changes: 125 additions & 0 deletions src/Kernel/Service/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* Copyright (c) 2012 Soflomo http://soflomo.com.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the names of the copyright holders nor the names of the
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Ensemble\KernelDoctrineORM
* @author Jurian Sluiman <jurian@soflomo.com>
* @copyright 2012 Soflomo http://soflomo.com.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://ensemble.github.com
*/

namespace Ensemble\Kernel\Service;

use Ensemble\Kernel\Mapper\PageInterface as PageMapperInterface;
use Ensemble\Kernel\Model\PageCollection as PageCollectionModel;
use Ensemble\Kernel\Model\PageInterface as PageModel;

/**
* Description of Page
*/
class Page
{
/**
* @var PageMapperInterface
*/
protected $pageMapper;


/**
* Constructor
*
* @param PageMapperInterface $pageMapper
*/
public function __construct(PageMapperInterface $pageMapper)
{
$this->pageMapper = $pageMapper;
}

/**
* Create a new page
*
* @param PageModel $page
* @return PageModel
*/
public function persist(PageModel $page)
{
return $this->pageMapper->persist($page);
}

/**
* Update an existing page
*
* @param PageModel $page
* @return PageModel
*/
public function update(PageModel $page)
{
return $this->pageMapper->update($page);
}

/**
* Delete an existing page
*
* @param PageModel $page
* @return bool
*/
public function delete(PageModel $page)
{
return $this->pageMapper->delete($page);
}

/**
* Find page based on id
*
* @param int $id
* @return Page
*/
public function find($id)
{
return $this->pageMapper->find($id);
}

/**
* Get the tree of pages
*
* @return PageCollectionModel
*/
public function getTree()
{
$pages = $this->pageMapper->getRootNodes('order', 'ASC');
$collection = new PageCollectionModel($pages);

return $collection;
}
}