AuthenticateICL.php
2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace WPML\ICLToATEMigration\Endpoints;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Json;
use WPML\FP\Logic;
use WPML\ICLToATEMigration\ICLStatus;
use WPML\TM\API\TranslationServices;
use WPML\TM\TranslationProxy\Services\AuthorizationFactory;
class AuthenticateICL implements IHandler {
/** @var TranslationServices */
private $translationServices;
/** @var ICLStatus */
private $iclStatus;
/**
* @param TranslationServices $translationServices
*/
public function __construct( TranslationServices $translationServices ) {
$this->translationServices = $translationServices;
$this->iclStatus = new ICLStatus( $translationServices );
}
public function run( Collection $data ) {
return Either::of( $data->get( 'token' ) )
->chain( Logic::ifElse(
Fns::identity(),
Either::of(),
Fns::always( Either::left( __( 'Token is not defined', 'sitepress-multilingual-cms' ) ) )
) )
->chain( Logic::ifElse(
[ $this->iclStatus, 'isActivatedAndAuthorized' ],
Fns::always( Either::left( __( 'ICanLocalize.com is already active and authorized', 'sitepress-multilingual-cms' ) ) ),
Either::of()
) )
->map( Logic::ifElse(
$this->isAnotherServiceCurrentlyActive(),
Fns::tap( [ $this->translationServices, 'deselect' ] ),
Fns::identity()
) )
->chain( Logic::ifElse(
[ $this->iclStatus, 'isActivated' ],
Either::of(),
function ( $token ) {
return $this->translationServices->selectBySUID( ICLStatus::SUID )->map( Fns::always( $token ) );
}
) )
->chain( [ $this->translationServices, 'authorize' ] )
->bimap( $this->errorResponse(), $this->successResponse() );
}
private function successResponse() {
return Fns::always( __( 'Service activated.', 'sitepress-multilingual-cms' ) );
}
private function errorResponse() {
return function ( $errorDetails = '' ) {
return [
'message' => __( 'The authentication didn\'t work. Please make sure you entered your details correctly and try again.', 'sitepress-multilingual-cms' ),
'details' => $errorDetails
];
};
}
private function isAnotherServiceCurrentlyActive() {
return function () {
return $this->translationServices->isAnyActive() && ! $this->iclStatus->isActivated();
};
}
}