Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
landrok committed Mar 5, 2019
1 parent 90573f5 commit acaca2d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ checks:
threshold: 10
method-complexity:
config:
threshold: 10
threshold: 20
method-count:
config:
threshold: 25
method-lines:
config:
threshold: 30
threshold: 50
nested-control-flow:
config:
threshold: 4
Expand Down
15 changes: 4 additions & 11 deletions src/ActivityPhp/Server/Actor/Inbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,10 @@ public function post(Request $request)

try {
// Check accept header
if (!Helper::validateAcceptHeader(
$request->headers->get('accept')
)
) {
throw new Exception(
sprintf(
"HTTP Accept header error. Given: '%s'",
$request->headers->get('accept')
)
);
}
Helper::validateAcceptHeader(
$request->headers->get('accept'),
true
);

// Check current actor can post

Expand Down
15 changes: 4 additions & 11 deletions src/ActivityPhp/Server/Actor/Outbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,10 @@ public function post(Request $request)
{
try {
// Check accept header
if (!Helper::validateAcceptHeader(
$request->headers->get('accept')
)
) {
throw new Exception(
sprintf(
"HTTP Accept header error. Given: '%s'",
$request->headers->get('accept')
)
);
}
Helper::validateAcceptHeader(
$request->headers->get('accept'),
true
);

// Check current actor can post

Expand Down
29 changes: 23 additions & 6 deletions src/ActivityPhp/Server/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,34 @@ abstract class Helper
* Validate HTTP Accept headers
*
* @param null|string|array $accept
* @param bool $strict Strict mode
* @return bool
* @throws \Exception when strict mode enabled
*/
public static function validateAcceptHeader($accept)
public static function validateAcceptHeader($accept, $strict = false)
{
if (is_string($accept)) {
return in_array($accept, self::$acceptHeaders);
} elseif (is_array($accept)) {
return count(
if (is_string($accept)
&& in_array($accept, self::$acceptHeaders)
) {
return true;
} elseif (is_array($accept)
&& count(
array_intersect($accept, self::$acceptHeaders)
) > 0;
)
) {
return true;
}

if (!$strict) {
return false;
}

throw new Exception(
sprintf(
"HTTP Accept header error. Given: '%s'",
$accept
)
);
}

/**
Expand Down

0 comments on commit acaca2d

Please sign in to comment.