Skip to content

Commit 005cb71

Browse files
authored
Merge pull request #23 from svenpet90/3-feature-implement-events-where-appropriate
[FEATURE] Dispatch events where appropriate
2 parents 90c504f + 2d9d20c commit 005cb71

File tree

6 files changed

+158
-11
lines changed

6 files changed

+158
-11
lines changed

Classes/Controller/PostController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use SvenPetersen\Instagram\Domain\Model\Post;
1515
use SvenPetersen\Instagram\Domain\Repository\PostRepository;
16+
use SvenPetersen\Instagram\Event\Controller\PreRenderActionEvent;
1617
use TYPO3\CMS\Core\Utility\GeneralUtility;
1718
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
1819

@@ -25,10 +26,20 @@ public function listAction(): void
2526
$posts = $postRepository->findBySettings($this->settings);
2627

2728
$this->view->assign('posts', $posts);
29+
30+
/** @var PreRenderActionEvent $event */
31+
$event = $this->eventDispatcher->dispatch(new PreRenderActionEvent($this->view, __METHOD__));
32+
33+
$this->view = $event->view;
2834
}
2935

3036
public function showAction(Post $post): void
3137
{
3238
$this->view->assign('post', $post);
39+
40+
/** @var PreRenderActionEvent $event */
41+
$event = $this->eventDispatcher->dispatch(new PreRenderActionEvent($this->view, __METHOD__));
42+
43+
$this->view = $event->view;
3344
}
3445
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Extension "instagram" for TYPO3 CMS.
7+
*
8+
* For the full copyright and license information, please read the
9+
* LICENSE.txt file that was distributed with this source code.
10+
*/
11+
12+
namespace SvenPetersen\Instagram\Event\Controller;
13+
14+
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
15+
16+
/**
17+
* This event can be used to modify the view object before it is rendered. E.g. add additional variables.
18+
*/
19+
class PreRenderActionEvent
20+
{
21+
public ViewInterface $view;
22+
23+
private string $action;
24+
25+
public function __construct(ViewInterface &$view, string $action)
26+
{
27+
$this->view = &$view;
28+
$this->action = $action;
29+
}
30+
31+
public function getAction(): string
32+
{
33+
return $this->action;
34+
}
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SvenPetersen\Instagram\Event\Post;
6+
7+
use SvenPetersen\Instagram\Domain\Model\Post;
8+
9+
class PostPersistPostEvent
10+
{
11+
private Post $post;
12+
13+
public function __construct(Post $post)
14+
{
15+
$this->post = $post;
16+
}
17+
18+
public function getPost(): Post
19+
{
20+
return $this->post;
21+
}
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SvenPetersen\Instagram\Event\Post;
6+
7+
use SvenPetersen\Instagram\Domain\Model\Post;
8+
9+
class PrePersistPostEvent
10+
{
11+
private Post $post;
12+
13+
private string $action;
14+
15+
public function __construct(Post $post, string $action)
16+
{
17+
$this->post = $post;
18+
$this->action = $action;
19+
}
20+
21+
public function getPost(): Post
22+
{
23+
return $this->post;
24+
}
25+
26+
public function setPost(Post $post): self
27+
{
28+
$this->post = $post;
29+
30+
return $this;
31+
}
32+
33+
public function getAction(): string
34+
{
35+
return $this->action;
36+
}
37+
}

Classes/Service/PostUpserter.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
namespace SvenPetersen\Instagram\Service;
1313

14+
use Psr\EventDispatcher\EventDispatcherInterface;
1415
use SvenPetersen\Instagram\Client\ApiClientInterface;
1516
use SvenPetersen\Instagram\Domain\Model\Dto\PostDTO;
1617
use SvenPetersen\Instagram\Domain\Model\Post;
1718
use SvenPetersen\Instagram\Domain\Repository\PostRepository;
19+
use SvenPetersen\Instagram\Event\Post\PostPersistPostEvent;
20+
use SvenPetersen\Instagram\Event\Post\PrePersistPostEvent;
1821
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
1922
use TYPO3\CMS\Core\Core\Environment;
2023
use TYPO3\CMS\Core\Database\ConnectionPool;
@@ -31,17 +34,21 @@ class PostUpserter
3134

3235
private PersistenceManagerInterface $persistenceManager;
3336

37+
private EventDispatcherInterface $eventDispatcher;
38+
3439
/**
3540
* @var array<string, string>
3641
*/
3742
private array $extConf;
3843

3944
public function __construct(
4045
PostRepository $postRepository,
41-
PersistenceManagerInterface $persistenceManager
46+
PersistenceManagerInterface $persistenceManager,
47+
EventDispatcherInterface $eventDispatcher
4248
) {
4349
$this->postRepository = $postRepository;
4450
$this->persistenceManager = $persistenceManager;
51+
$this->eventDispatcher = $eventDispatcher;
4552

4653
/** @var ExtensionConfiguration $extensionConfiguration */
4754
$extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class);
@@ -62,10 +69,16 @@ public function upsertPost(PostDTO $dto, int $storagePid, ApiClientInterface $ap
6269
->setFeed($apiClient->getFeed())
6370
->setPid($storagePid);
6471

72+
/** @var PrePersistPostEvent $event */
73+
$event = $this->eventDispatcher->dispatch(new PrePersistPostEvent($post, $action));
74+
$post = $event->getPost();
75+
6576
$this->postRepository->add($post);
6677
$this->persistenceManager->persistAll();
6778

6879
if ($action === 'UPDATE') {
80+
$this->eventDispatcher->dispatch(new PostPersistPostEvent($post));
81+
6982
return $post;
7083
}
7184

@@ -117,6 +130,8 @@ private function processPostMedia(Post $post, PostDTO $dto): Post
117130
$this->postRepository->update($post);
118131
$this->persistenceManager->persistAll();
119132

133+
$this->eventDispatcher->dispatch(new PostPersistPostEvent($post));
134+
120135
return $post;
121136
}
122137

@@ -186,8 +201,6 @@ private function upsertFromDTO(PostDTO $dto, Post $post = null): Post
186201
}
187202

188203
/**
189-
* todo: add test
190-
*
191204
* @return string[]
192205
*/
193206
private function extractHashtags(string $text): array

README.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ TYPO3 Extension "instagram"
1111
=================================
1212

1313
## What does it do?
14+
1415
TYPO3 Extension to import and display instagram posts/feeds in a TYPO3 Website.
1516

1617
Creates and auto-refreshes long-lived api access tokens, imports
@@ -23,16 +24,22 @@ Frontend-Plugin.
2324
* Automatic refreshing of API access tokens to keep them valid
2425
* Import multiple Instagram users feeds/posts
2526
* Uses official Instagram-API to access a users feed.
26-
* Provides commands to refresh access tokens and to import feeds via cronjob/scheduler
27-
* Downloads the posts (images/videos). No API calls needed when displaying them = no frontend performance impact.
27+
* Provides commands to refresh access tokens and to import feeds via
28+
cronjob/scheduler
29+
* Downloads the posts (images/videos). No API calls needed when displaying
30+
them = no frontend performance impact.
2831
* Display users feeds/posts in any way you like
2932
* Based on Extbase and Fluid
3033

3134
## Installation
32-
The recommended way to install the extension is by using [Composer](https://getcomposer.org/). In your Composer based TYPO3 project root, just do:
35+
36+
The recommended way to install the extension is by
37+
using [Composer](https://getcomposer.org/). In your Composer based TYPO3 project
38+
root, just do:
3339
<pre>composer require svenpetersen/instagram</pre>
3440

3541
## Setup
42+
3643
1. Include the provided static TypoScript
3744
2. Create a Facebook "Instagram Basic Display" App: See the
3845
[official Documentation](https://developers.facebook.com/docs/instagram-basic-display-api/getting-started)
@@ -50,13 +57,15 @@ __Recommended__:
5057
* Add a cronjob/scheduler task to import the posts on a regular basis
5158

5259
## Compatibility
60+
5361
| Version | TYPO3 | PHP | Support/Development |
5462
|---------|-------------|------------|--------------------------------------|
5563
| 1.x | 10.4 - 11.5 | 7.4 - 8.0️ | Features, Bugfixes, Security Updates |
5664

5765
## Funtionalities
5866

5967
### Automatic import of posts
68+
6069
This extension comes with a command to import (new) posts of a given instagram
6170
user.
6271
It is recommended to set this command up to run regularly - e.g. once a day.
@@ -79,8 +88,8 @@ __Options:__
7988
| --since | Date string to fetch posts since (Format: "MM/DD/YYYY H:i:s"). |
8089
| --until | Date string to fetch posts until (Format: "MM/DD/YYYY H:i:s"). |
8190

82-
8391
### Automatic Access Token Refreshing
92+
8493
The generated long-lived access token is valid for 60 days.
8594
It can be refreshed when at least 24 hours old.
8695

@@ -92,6 +101,7 @@ Make sure to run this command regularly - e.g. once a day via a
92101
cronjob/scheduler - in order to keep your access token valid.
93102

94103
### Disable/enable Backend module "Token Generator"
104+
95105
To disable the Backend module - e.g. after you generated all needed tokens - add
96106
this
97107
line to the <code>LocalConfiguration.php/AdditionalConfiguration.php</code>:
@@ -100,17 +110,36 @@ line to the <code>LocalConfiguration.php/AdditionalConfiguration.php</code>:
100110
## Extending
101111

102112
### Additional Template Selector
103-
If you need a kind of template selector inside a plugin, you can add your own selections by adding those to:
113+
114+
If you need a kind of template selector inside a plugin, you can add your own
115+
selections by adding those to:
104116
<pre>$GLOBALS['TYPO3_CONF_VARS']['EXT']['instagram']['templateLayouts']['myext'] = ['My Title', 'my value'];</pre>
105117

106118
### Local path to save downloaded files
107-
By default all images/videos in imported posts are saved in <code>/public/fileadmin/instagram</code>
108-
You can change this path via the Extensions settings <code>local_file_storage_path</code> option.
119+
120+
By default all images/videos in imported posts are saved in <code>
121+
/public/fileadmin/instagram</code>
122+
You can change this path via the Extensions settings <code>
123+
local_file_storage_path</code> option.
124+
125+
### Events
126+
127+
This extension comes with a few events for you to listen to and add your own
128+
logic:
129+
130+
| Name | Args. | Description |
131+
|----------------------|---------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
132+
| PreRenderActionEvent | ViewInterface $view <br> string $action | Dispatched before the view is rendered. Can be used to modify the view object. E.g. adding additional vars to the frontend (e.g. pagination) |
133+
| PrePersistPostEvent | Post $post <br> string $action['new' or ‘update'] | Dispatched before a Post is saved. Can be used to modify the Post entity. | Dispatched before the view is rendered. Can be used to modify the view object. E.g. adding additional vars to the frontend (e.g. pagination) |
134+
| PostPersistPostEvent | Post $post | Dispatched after a Post is saved. | Dispatched before the view is rendered. Can be used to modify the view object. E.g. adding additional vars to the frontend (e.g. pagination) |
109135

110136
## Contributing
111-
Please refer to the [contributing](CONTRIBUTING.md) document included in this repository.
137+
138+
Please refer to the [contributing](CONTRIBUTING.md) document included in this
139+
repository.
112140

113141
## Testing
142+
114143
This Extension comes with a testsuite for coding styles and unit/functional
115144
tests.
116145
To run the tests simply use the provided composer script:

0 commit comments

Comments
 (0)