Completed
Pull Request — master (#32545)
by Tom
09:55
created

TransferOwnership   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A execute() 0 4 1
B run() 0 42 6
1
<?php
2
3
namespace OCA\Files\BackgroundJob;
4
5
use OC\BackgroundJob\QueuedJob;
6
use OCA\Files\Service\TransferOwnership\TransferOwnershipService;
7
use OCA\Files\Service\TransferOwnership\TransferRequestManager;
8
use OCP\AppFramework\Db\DoesNotExistException;
9
use OCP\Files\IRootFolder;
10
use OCP\Files\NotFoundException;
11
use OCP\ILogger;
12
use OCP\IUserManager;
13
14
class TransferOwnership extends QueuedJob {
15
16
	/** @var ILogger */
17
	protected $logger;
18
	/** @var TransferOwnershipService  */
19
	protected $service;
20
	/** @var IUserManager  */
21
	protected $userManager;
22
	/** @var TransferRequestManager  */
23
	protected $requestManager;
24
	/** @var IRootFolder  */
25
	protected $rootFolder;
26
27
	public function __construct(
28
		TransferOwnershipService $service,
29
		IUserManager $userManager,
30
		TransferRequestManager $requestManager,
31
		IRootFolder $rootFolder) {
32
		$this->service = $service;
33
		$this->userManager = $userManager;
34
		$this->requestManager = $requestManager;
35
		$this->rootFolder = $rootFolder;
36
	}
37
38
	public function execute($jobList, ILogger $logger = null) {
39
		$this->logger = $logger;
40
		parent::execute($jobList, $logger);
0 ignored issues
show
Compatibility introduced by
$jobList of type object<OCP\BackgroundJob\IJobList> is not a sub-type of object<OC\BackgroundJob\JobList>. It seems like you assume a concrete implementation of the interface OCP\BackgroundJob\IJobList to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
41
	}
42
43
	public function run($argument) {
44
		// Get the arguments;
45
		$arguments = json_decode($argument);
46
		try {
47
			$request = $this->requestManager->getRequestById($arguments->requestId);
48
		} catch (DoesNotExistException $e) {
49
			// Can happen if this gets deleted before the cron runs
50
			$this->logger->info("Transfer job called but request is missing");
51
			return;
52
		}
53
54
		// Call the transfer service
55
		$sourceUser = $this->userManager->get($request->getSourceUserId());
0 ignored issues
show
Documentation Bug introduced by
The method getSourceUserId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56
		$destinationUser = $this->userManager->get($request->getDestinationUserId());
0 ignored issues
show
Documentation Bug introduced by
The method getDestinationUserId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
57
58
		if ($sourceUser === null || $destinationUser === null) {
59
			$this->logger->error("Trasnfer job called but at least one of the users in the request are missing: source:{$request->getSourceUserId()} destination:{$request->getDestinationUserId()}", ['app' => 'files']);
0 ignored issues
show
Documentation Bug introduced by
The method getSourceUserId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Documentation Bug introduced by
The method getDestinationUserId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
60
			return;
61
		}
62
63
		try {
64
			$sourcePath = $this->rootFolder->getUserFolder($request->getSourceUserId())
0 ignored issues
show
Documentation Bug introduced by
The method getSourceUserId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
65
				->getById($request->getFileId())[0]
0 ignored issues
show
Documentation Bug introduced by
The method getFileId does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
66
				->getInternalPath();
67
		} catch (NotFoundException $e) {
68
			$this->logger->error("Transfer job called but node no longer exists");
69
			$this->requestManager->deleteRequest($request);
0 ignored issues
show
Compatibility introduced by
$request of type object<OCP\AppFramework\Db\Entity> is not a sub-type of object<OCA\Files\Service...ership\TransferRequest>. It seems like you assume a child class of the class OCP\AppFramework\Db\Entity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
70
			return;
71
		}
72
73
74
		try {
75
			$this->service->transfer($sourceUser, $destinationUser, $sourcePath);
76
		} catch (\Exception $e) {
77
			$this->logger->logException($e, ['app' =>  'files']);
78
			return;
79
		}
80
81
		$this->logger->info("Finished transfer of $sourcePath from {$sourceUser->getUID()} to {$destinationUser->getUID()}");
82
		$this->requestManager->actionRequest($request);
0 ignored issues
show
Compatibility introduced by
$request of type object<OCP\AppFramework\Db\Entity> is not a sub-type of object<OCA\Files\Service...ership\TransferRequest>. It seems like you assume a child class of the class OCP\AppFramework\Db\Entity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
83
84
	}
85
86
}