Skip to content

Commit

Permalink
Sending messages to forums
Browse files Browse the repository at this point in the history
  • Loading branch information
chaker2710 committed Mar 26, 2022
1 parent c3ff390 commit fb67857
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Endpoints/Forums.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace OvaStudio\IpsApi\Endpoints;

use OvaStudio\IpsApi\Endpoints\Forums\Posts;

class Forums extends AbstractEndpoint
{
public function posts() : Posts
{
return new Posts($this->api);
}
}
59 changes: 59 additions & 0 deletions src/Endpoints/Forums/Posts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace OvaStudio\IpsApi\Endpoints\Forums;

use OvaStudio\IpsApi\Endpoints\AbstractEndpoint;
use OvaStudio\IpsApi\IpsApi;
use stdClass;

class Posts extends AbstractEndpoint
{
private const ENDPOINT = 'forums/posts';

private int $author;
private ?string $author_name;
private bool $anonymous = false;

public function __construct(IpsApi $api)
{
parent::__construct($api);

$this->withAuthor(config('services.ips.default_user', 1));
}

public function withAuthor(int $author, ?string $author_name = null) : static
{
$this->author = $author;
$this->author_name = $author_name;

return $this;
}

public function anonymousMode(bool $anonymous = true) : static
{
$this->anonymous = $anonymous;

return $this;
}

public function create(int $topic, string $post) : stdClass
{
$data = [
'topic' => $topic,
'author' => $this->author,
'post' => $post,
];

if ($data['author'] === 0)
$data['author_name'] = $this->author_name;

if ($this->anonymous)
$data['anonymous'] = 1;

$response = $this->api->client->post(self::ENDPOINT, [
'form_params' => $data
]);

return json_decode($response->getBody());
}
}
6 changes: 6 additions & 0 deletions src/IpsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Client;
use OvaStudio\IpsApi\Endpoints\Forums;
use OvaStudio\IpsApi\Endpoints\System;
use OvaStudio\IpsApi\Exceptions\AppUnavailableException;
use OvaStudio\IpsApi\Exceptions\BadRequestException;
Expand Down Expand Up @@ -167,4 +168,9 @@ public function system() : System
{
return new System($this);
}

public function forums() : Forums
{
return new Forums($this);
}
}
17 changes: 17 additions & 0 deletions tests/Feature/Endpoints/ForumsPostsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace OvaStudio\IpsApi\Tests\Feature\Endpoints;

use OvaStudio\IpsApi\IpsApi;
use OvaStudio\IpsApi\Tests\TestCase;

class ForumsPostsTest extends TestCase
{
public function test_post_create()
{
$post = (new IpsApi())->forums()->posts()->create(2928, '<p>Test</p>');

self::assertTrue(true);
self::assertNotNull($post->id);
}
}

0 comments on commit fb67857

Please sign in to comment.