Yesterday I had a discussion at the IRC Symfony2 channel about best practice approaches and tutorials (book, cookbook on sf2-website).
Someone said services have always to be stateless. He meant that there should be no connection to an Entity in a service class. But how should I work with user generated content if I need the content from database preformatted?
Although someone said the service should be stateless I created a service and a factory service based on this tutorial: http://brentertainment.com/2012/02/28/contextualizing-your-symfony2-application-with-the-service-container/
The naming of my configuration services is currently a bit bad, but I have no strategy or good naming concept in the moment.
Currently I call $this->get('test.conf')->getCategory('data-browser')
in the Controller. It "feels good" to do this by that way, because I don't have to repeat the function in the controller.
The service test.conf then calls the factory service test.conf_factory, which get an instance of service.configuration.
Questions:
Are services the right place to get preformatted content like I do here?
Is the service configuration correct? Or could I simplify test.conf_factory
, test.conf
and test.configuration
in any way?
Currently I really don't know if I'm doing it right.
Would it be better (or is it possible) to use the service test.configuration
and create another service which just creates in the constructor an instance of test.configuration and returns by an call of the method 'get' the instance? (http://symfony.com/doc/current/components/dependency_injection/introduction.html)
Is there a proved way like most of the symfony developers do that?
The configuration factory:
namespace Test\CoreBundle\Factory;
use Test\CoreBundle\Entity\Account;
use Test\CoreBundle\Service\Configuration;
class ConfigurationFactory
{
private $user;
private $configuration;
public function __construct(Account $user = null, Configuration $configuration)
{
$this->user = $user;
dump($user);
$this->configuration = $configuration;
}
public function get()
{
if ($this->user) {
// dump($this->user);
$this->configuration->setAccountid($this->user->getId());
}
return $this->configuration;
}
}
The configuration serivce:
<?php
namespace test\CoreBundle\Service;
class Configuration {
private $em;
private $category;
private $accountid;
/**
* Values from database
* @var array
*/
private $ValuesFromDB = null;
public function __construct($em)
{
$this->em = $em->getRepository('TestCoreBundle:Conf');
}
public function setAccountid($accountid) {
$this->accountid = $accountid;
$this->loadAll();
}
public function loadAll() {
$DBconf = $this->em->findAllByAccount($this->accountid);
foreach($DBconf as $key => $conf) {
$values[$conf->getCategory()][$conf->getKey()] = $conf->getValue();
}
$this->ValuesFromDB = $values;
}
public function setCategory($category) {
$this->category = $category;
}
public function getCategory($category) {
return $this->ValuesFromDB[$category];
}
public function get($key) {
return $this->ValuesFromDB[$this->category][$key];
}
}
services:
test.account_factory:
class: test\CoreBundle\Factory\AccountFactory
arguments: [@security.context]
test.account:
class: test\CoreBundle\Entity\Account
factory_service: test.account_factory
factory_method: get
test.configuration:
class: test\CoreBundle\Service\Configuration
arguments: [@doctrine.orm.entity_manager]
test.conf_factory:
class: test\CoreBundle\Factory\ConfigurationFactory
arguments:
- @test.account
- @test.configuration
test.conf:
class: Configuration
factory_service: test.conf_factory
factory_method: get
I'm not sure if I understand your design issues very well, but I guess that what you are actually trying to do is to retrieve some data from your database according to a specific entity (here, your user). I'll try then to share you my opinion :
What I can see from your design is that you misunderstood the Factory concept : here, it is clearly useless.
Then, I would try to rething your tool by thinking : what should my tool do ? Don't you think that the design that you're implementing is too complex ?
If I understand what you want to do well, I guess that you could use your tool by :
Your questions in the beginning of your post are actually not meant to be asked, because you should not inject an instance of Account in your class. In the end, something like :
// I get my account
$myAccount = $this->getEntityManager()->getRepository('TestCoreBundle:Conf')->find(/* ... */);
$configurationManager = $this->get('configuration_manager');
// The manager permits me to get the "extra stuff" related to this account.
$confStuff = $configurationManager->getConfigurationStuffFromAccount($myAccount);
... Should be the way you could fix your issues, if I understood it well.
That way :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With