[refactoring] moving UpdateManger to service container as util.server.updatemanager

This commit is contained in:
Pepijn Over 2015-02-27 15:05:46 +01:00
parent f63a39d92c
commit dc54bf14ea
4 changed files with 23 additions and 39 deletions

View File

@ -68,7 +68,7 @@ if(!defined('PSM_DEBUG') || !PSM_DEBUG) {
} }
psm_update_conf('cron_running_time', $time); psm_update_conf('cron_running_time', $time);
$autorun = new \psm\Util\Server\UpdateManager($db); $autorun = $router->getService('util.server.updatemanager');
$autorun->run(); $autorun->run(true);
psm_update_conf('cron_running', 0); psm_update_conf('cron_running', 0);

View File

@ -32,6 +32,9 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/sc
<argument>%db.pass%</argument> <argument>%db.pass%</argument>
</service> </service>
<service id="event" class="Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher">
<argument type="service" id="service_container" />
</service>
<service id="user" class="psm\Service\User"> <service id="user" class="psm\Service\User">
<argument type="service" id="db" /> <argument type="service" id="db" />
</service> </service>
@ -47,6 +50,9 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/sc
<service id="util.user.validator" class="psm\Util\User\UserValidator"> <service id="util.user.validator" class="psm\Util\User\UserValidator">
<argument type="service" id="user" /> <argument type="service" id="user" />
</service> </service>
<service id="util.server.updatemanager" class="psm\Util\Server\UpdateManager">
<argument type="service" id="service_container" />
</service>
<!--UTIL end--> <!--UTIL end-->
</services> </services>
</container> </container>

View File

@ -40,8 +40,7 @@ class UpdateController extends AbstractController {
} }
protected function executeIndex() { protected function executeIndex() {
$autorun = new \psm\Util\Server\UpdateManager($this->db); $autorun = $this->container->get('util.server.updatemanager');
$autorun->setUser($this->getUser());
$autorun->run(); $autorun->run();
header('Location: ' . psm_build_url(array( header('Location: ' . psm_build_url(array(

View File

@ -27,44 +27,31 @@
**/ **/
namespace psm\Util\Server; namespace psm\Util\Server;
use psm\Service\Database; use Symfony\Component\DependencyInjection\ContainerAware;
use psm\Service\User; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Run an update on all servers. * Run an update on all servers.
*
* If you provide a User service instance it will be
* restricted to that user only.
*/ */
class UpdateManager { class UpdateManager extends ContainerAware {
/** function __construct(ContainerInterface $container) {
* Database service $this->container = $container;
* @var \psm\Service\Database $db
*/
protected $db;
/**
* User service
* @var \psm\Service\User $user
*/
protected $user;
function __construct(Database $db) {
$this->db = $db;
} }
/** /**
* Go :-) * Go :-)
*
* @param boolean $skip_perms if TRUE, no user permissions will be taken in account and all servers will be updated
*/ */
public function run() { public function run($skip_perms = false) {
// check if we need to restrict the servers to a certain user // check if we need to restrict the servers to a certain user
$sql_join = ''; $sql_join = '';
if($this->user != null && $this->user->getUserLevel() > PSM_USER_ADMIN) { if(!$skip_perms && $this->container->get('user')->getUserLevel() > PSM_USER_ADMIN) {
// restrict by user_id // restrict by user_id
$sql_join = "JOIN `".PSM_DB_PREFIX."users_servers` AS `us` ON ( $sql_join = "JOIN `".PSM_DB_PREFIX."users_servers` AS `us` ON (
`us`.`user_id`={$this->user->getUserId()} `us`.`user_id`={$this->container->get('user')->getUserId()}
AND `us`.`server_id`=`s`.`server_id` AND `us`.`server_id`=`s`.`server_id`
)"; )";
} }
@ -74,10 +61,10 @@ class UpdateManager {
{$sql_join} {$sql_join}
WHERE `active`='yes' "; WHERE `active`='yes' ";
$servers = $this->db->query($sql); $servers = $this->container->get('db')->query($sql);
$updater = new Updater\StatusUpdater($this->db); $updater = new Updater\StatusUpdater($this->container->get('db'));
$notifier = new Updater\StatusNotifier($this->db); $notifier = new Updater\StatusNotifier($this->container->get('db'));
foreach($servers as $server) { foreach($servers as $server) {
$status_old = ($server['status'] == 'on') ? true : false; $status_old = ($server['status'] == 'on') ? true : false;
@ -87,16 +74,8 @@ class UpdateManager {
} }
// clean-up time!! archive all records // clean-up time!! archive all records
$archive = new ArchiveManager($this->db); $archive = new ArchiveManager($this->container->get('db'));
$archive->archive(); $archive->archive();
$archive->cleanup(); $archive->cleanup();
} }
/**
* Set a user to restrict the servers being updated
* @param \psm\Service\User $user
*/
public function setUser(User $user) {
$this->user = $user;
}
} }