Skip to content

Commit 3a3e8a6

Browse files
committed
Merge branch 'b-7.3.x-controller-as-service-example-OXDEV-9497' into b-7.3.x
2 parents 8f498a8 + 450b0f9 commit 3a3e8a6

19 files changed

Lines changed: 351 additions & 97 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ The repository contains examples of following cases and more:
6060
* extending a shop model (`OxidEsales\ExamplesModule\Extension\Model\User`) / (`OxidEsales\ExamplesModule\Extension\Model\Basket`)
6161
* extending a shop controller (`OxidEsales\ExamplesModule\Extension\Controller\StartController`)
6262

63-
* [New controllers](https://github.com/OXID-eSales/examples-module/blob/b-7.3.x/metadata.php#L30)
64-
* own module controller (`oeemgreeting` with own template and own translations)
63+
* [Controllers as service](https://github.com/OXID-eSales/examples-module/blob/b-7.3.x/src/Greeting/services.yaml#L28)
64+
* own module controller (`oeem_greeting` with own template and own translations)
6565
* own module admin controller (`oeem_admin_greeting` with own template and own translations)
6666

6767
* [Using Symfony DI](https://github.com/OXID-eSales/examples-module/blob/b-7.3.x/services.yaml)

metadata.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
\OxidEsales\Eshop\Application\Model\Basket::class => \OxidEsales\ExamplesModule\Extension\Model\Basket::class,
3030
\OxidEsales\Eshop\Application\Model\User::class => \OxidEsales\ExamplesModule\Extension\Model\User::class,
3131
],
32-
'controllers' => [
33-
'oeem_greeting' => \OxidEsales\ExamplesModule\Greeting\Controller\GreetingController::class,
34-
'oeem_admin_greeting' => \OxidEsales\ExamplesModule\Greeting\Controller\Admin\GreetingAdminController::class,
35-
],
3632
'events' => [
3733
'onActivate' => '\OxidEsales\ExamplesModule\Core\ModuleEvents::onActivate',
3834
'onDeactivate' => '\OxidEsales\ExamplesModule\Core\ModuleEvents::onDeactivate'

src/Extension/Model/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* @mixin BaseModel
2727
*/
28-
class User extends User_parent implements PersonalGreetingUserInterface
28+
class User extends User_parent implements UserInterface
2929
{
3030
use PersonalGreetingUser;
3131
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
namespace OxidEsales\ExamplesModule\Extension\Model;
9+
10+
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUserInterface;
11+
12+
interface UserInterface extends PersonalGreetingUserInterface
13+
{
14+
}

src/Greeting/Controller/Admin/GreetingAdminController.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,25 @@
1313
use OxidEsales\Eshop\Application\Controller\Admin\AdminController;
1414
use OxidEsales\ExamplesModule\Extension\Model\User as ExamplesModelUser;
1515
use OxidEsales\ExamplesModule\Greeting\Service\UserServiceInterface;
16+
use OxidEsales\ExamplesModule\Greeting\Transput\AdminGreetingRequestInterface;
1617

1718
class GreetingAdminController extends AdminController
1819
{
1920
protected $_sThisTemplate = '@oe_examples_module/admin/user_greetings';
2021

22+
public function __construct(
23+
private readonly UserServiceInterface $userService,
24+
private readonly AdminGreetingRequestInterface $request,
25+
) {
26+
parent::__construct();
27+
}
28+
2129
public function render()
2230
{
23-
$userService = $this->getService(UserServiceInterface::class);
24-
if ($this->getEditObjectId()) {
25-
/** @var ExamplesModelUser $oUser */
26-
$oUser = $userService->getUserById($this->getEditObjectId());
27-
$this->addTplParam(ModuleCore::OEEM_ADMIN_GREETING_TEMPLATE_VARNAME, $oUser->getPersonalGreeting());
31+
if ($this->request->getEditObjectId()) {
32+
/** @var ExamplesModelUser $user */
33+
$user = $this->userService->getUserById($this->request->getEditObjectId());
34+
$this->addTplParam(ModuleCore::OEEM_ADMIN_GREETING_TEMPLATE_VARNAME, $user->getPersonalGreeting());
2835
}
2936

3037
return parent::render();

src/Greeting/Controller/GreetingController.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class GreetingController extends FrontendController
3333
*/
3434
protected $_sThisTemplate = '@oe_examples_module/templates/greetingtemplate';
3535

36+
public function __construct(
37+
private readonly ModuleSettingsServiceInterface $moduleSettings,
38+
private readonly TrackerRepositoryInterface $trackerRepository,
39+
private readonly GreetingMessageServiceInterface $greetingService,
40+
) {
41+
parent::__construct();
42+
}
43+
3644
/**
3745
* Rendering method.
3846
*
@@ -41,16 +49,14 @@ class GreetingController extends FrontendController
4149
public function render()
4250
{
4351
$template = parent::render();
44-
$moduleSettings = $this->getService(ModuleSettingsServiceInterface::class);
45-
$repository = $this->getService(TrackerRepositoryInterface::class);
4652

4753
/** @var ExamplesModelUser $user */
4854
$user = $this->getUser();
4955

5056
/** @phpstan-ignore-next-line */
51-
if (is_a($user, EshopModelUser::class) && $moduleSettings->isPersonalGreetingMode()) {
57+
if (is_a($user, EshopModelUser::class) && $this->moduleSettings->isPersonalGreetingMode()) {
5258
$greeting = $user->getPersonalGreeting();
53-
$tracker = $repository->getTrackerByUserId($user->getId());
59+
$tracker = $this->trackerRepository->getTrackerByUserId($user->getId());
5460
$counter = $tracker->getCount();
5561
}
5662

@@ -68,15 +74,12 @@ public function render()
6874
*/
6975
public function updateGreeting(): void
7076
{
71-
$moduleSettings = $this->getService(ModuleSettingsServiceInterface::class);
72-
7377
/** @var EshopModelUser $user */
7478
$user = $this->getUser();
7579

7680
/** @phpstan-ignore-next-line */
77-
if (is_a($user, EshopModelUser::class) && $moduleSettings->isPersonalGreetingMode()) {
78-
$greetingService = $this->getService(GreetingMessageServiceInterface::class);
79-
$greetingService->saveGreeting($user);
81+
if (is_a($user, EshopModelUser::class) && $this->moduleSettings->isPersonalGreetingMode()) {
82+
$this->greetingService->saveGreeting($user);
8083
}
8184
}
8285
}

src/Greeting/Infrastructure/UserModelFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
class UserModelFactory implements UserModelFactoryInterface
1515
{
16+
/**
17+
* @return \OxidEsales\ExamplesModule\Extension\Model\User
18+
*/
1619
public function create(): User
1720
{
21+
/** @phpstan-ignore return.type */
1822
return oxNew(User::class);
1923
}
2024
}

src/Greeting/Infrastructure/UserModelFactoryInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111

1212
interface UserModelFactoryInterface
1313
{
14+
/**
15+
* @return \OxidEsales\ExamplesModule\Extension\Model\User
16+
*/
1417
public function create(): User;
1518
}

src/Greeting/Service/UserService.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99

1010
namespace OxidEsales\ExamplesModule\Greeting\Service;
1111

12-
use OxidEsales\Eshop\Application\Model\User as EshopModelUser;
1312
use OxidEsales\ExamplesModule\Greeting\Infrastructure\UserModelFactoryInterface;
13+
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUserInterface;
1414

1515
/**
1616
* @extendable-class
17+
*
18+
* @todo: getting the user should go through the user repository
1719
*/
1820
readonly class UserService implements UserServiceInterface
1921
{
@@ -22,7 +24,7 @@ public function __construct(
2224
) {
2325
}
2426

25-
public function getUserById(string $userId): EshopModelUser
27+
public function getUserById(string $userId): PersonalGreetingUserInterface
2628
{
2729
$userModel = $this->userModelFactory->create();
2830
$userModel->load($userId);

src/Greeting/Service/UserServiceInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace OxidEsales\ExamplesModule\Greeting\Service;
1111

12-
use OxidEsales\Eshop\Application\Model\User as EshopModelUser;
12+
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUserInterface;
1313

1414
interface UserServiceInterface
1515
{
16-
public function getUserById(string $userId): EshopModelUser;
16+
public function getUserById(string $userId): PersonalGreetingUserInterface;
1717
}

0 commit comments

Comments
 (0)