]> git.mxchange.org Git - friendica.git/blob - src/Console/GlobalCommunityBlock.php
The value is used twice, so use a variable
[friendica.git] / src / Console / GlobalCommunityBlock.php
1 <?php
2
3 namespace Friendica\Console;
4
5 use Friendica\App;
6 use Friendica\Core\L10n;
7 use Friendica\Model\Contact;
8
9 /**
10  * @brief tool to block an account from the node
11  *
12  * With this tool, you can block an account in such a way, that no postings
13  * or comments this account writes are accepted to the node.
14  *
15  * License: AGPLv3 or later, same as Friendica
16  *
17  * @author Tobias Diekershoff <tobias.diekershoff@gmx.net>
18  * @author Hypolite Petovan <hypolite@mrpetovan.com>
19  */
20 class GlobalCommunityBlock extends \Asika\SimpleConsole\Console
21 {
22         protected $helpOptions = ['h', 'help', '?'];
23
24         /**
25          * @var App\Mode
26          */
27         private $appMode;
28         /**
29          * @var L10n\L10n
30          */
31         private $l10n;
32
33         protected function getHelp()
34         {
35                 $help = <<<HELP
36 console globalcommunityblock - Block remote profile from interacting with this node
37 Usage
38         bin/console globalcommunityblock <profile_url> [<reason>] [-h|--help|-?] [-v]
39
40 Description
41         Blocks an account in such a way that no postings or comments this account writes are accepted to this node.
42         You can provide a optional reason for the block.
43
44 Options
45     -h|--help|-? Show help information
46     -v           Show more debug information.
47 HELP;
48                 return $help;
49         }
50
51         public function __construct(App\Mode $appMode, L10n $l10n, $argv = null)
52         {
53                 parent::__construct($argv);
54
55                 $this->appMode = $appMode;
56                 $this->l10n = $l10n;
57         }
58
59         protected function doExecute()
60         {
61                 if ($this->getOption('v')) {
62                         $this->out('Class: ' . __CLASS__);
63                         $this->out('Arguments: ' . var_export($this->args, true));
64                         $this->out('Options: ' . var_export($this->options, true));
65                 }
66
67                 if (count($this->args) == 0) {
68                         $this->out($this->getHelp());
69                         return 0;
70                 }
71
72                 if (count($this->args) > 2) {
73                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
74                 }
75
76                 if ($this->appMode->isInstall()) {
77                         throw new \RuntimeException('Database isn\'t ready or populated yet');
78                 }
79
80                 $contact_id = Contact::getIdForURL($this->getArgument(0));
81                 if (!$contact_id) {
82                         throw new \RuntimeException($this->l10n->t('Could not find any contact entry for this URL (%s)', $this->getArgument(0)));
83                 }
84
85                 $block_reason = $this->getArgument(1);
86                 if(Contact::block($contact_id, $block_reason)) {
87                         $this->out($this->l10n->t('The contact has been blocked from the node'));
88                 } else {
89                         throw new \RuntimeException('The contact block failed.');
90                 }
91
92                 return 0;
93         }
94 }